Testing for the first few letters of a string
Okay, I have a fix for this problem, but it is messy and I think there might be a better way. Heres an example: Lets say I have a string: My name is alex and I have another string My name is alex, and I like pie. I want to test to see if just the My name is alex part is there. I don't care about the pie part. My first instinct was to just create a for loop and test for the string like this: n = 0 for x in string1: if string1[n] == string2[n] n = n +0 else: break and then later testing to see what n was = to and figuring out if it got through the whole loop. I feel like there should be an easier way to do this, and probably is. So Does anyone have a suggestion? -- View this message in context: http://www.nabble.com/Testing-for-the-first-few-letters-of-a-string-tp18873375p18873375.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing for the first few letters of a string
Check out the built-in string.startswith() method. -- http://mail.python.org/mailman/listinfo/python-list
RE: Testing for the first few letters of a string
use re module import re template = '^My name is alex' astring = 'My name is alex, and I like pie' if re.match(template, astring): print 'Found it' else: print '%s does not begin with %s' % (astring, template) good luck. Edwin -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alexnb Sent: Thursday, August 07, 2008 11:40 AM To: python-list@python.org Subject: Testing for the first few letters of a string Okay, I have a fix for this problem, but it is messy and I think there might be a better way. Heres an example: Lets say I have a string: My name is alex and I have another string My name is alex, and I like pie. I want to test to see if just the My name is alex part is there. I don't care about the pie part. My first instinct was to just create a for loop and test for the string like this: n = 0 for x in string1: if string1[n] == string2[n] n = n +0 else: break and then later testing to see what n was = to and figuring out if it got through the whole loop. I feel like there should be an easier way to do this, and probably is. So Does anyone have a suggestion? -- View this message in context: http://www.nabble.com/Testing-for-the-first-few-letters-of-a-string-tp18873375p18873375.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list The information contained in this message and any attachment may be proprietary, confidential, and privileged or subject to the work product doctrine and thus protected from disclosure. If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify me immediately by replying to this message and deleting it and all copies and backups thereof. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing for the first few letters of a string
try string1 = My name is alex string2 = My name is alex, and I like pie if string2.startswith(string1): process() or if you want to match a set number of characters you can use a slice: if string2[:15] == string1[:15]: process() or if you dont care where the characters appear in the string, beginning, middle, end, etc: if string2 in string1: process() Theres lots of other ways as well. http://docs.python.org/lib/string-methods.html ~Sean On Aug 7, 8:40 am, Alexnb [EMAIL PROTECTED] wrote: Okay, I have a fix for this problem, but it is messy and I think there might be a better way. Heres an example: Lets say I have a string: My name is alex and I have another string My name is alex, and I like pie. I want to test to see if just the My name is alex part is there. I don't care about the pie part. My first instinct was to just create a for loop and test for the string like this: n = 0 for x in string1: if string1[n] == string2[n] n = n +0 else: break and then later testing to see what n was = to and figuring out if it got through the whole loop. I feel like there should be an easier way to do this, and probably is. So Does anyone have a suggestion? -- View this message in context:http://www.nabble.com/Testing-for-the-first-few-letters-of-a-string-t... Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing for the first few letters of a string
On Aug 8, 1:53 am, [EMAIL PROTECTED] wrote: use re module Using re.match instead of str.startswith is overkill. import re template = '^My name is alex' The ^ is redundant. astring = 'My name is alex, and I like pie' if re.match(template, astring): print 'Found it' else: print '%s does not begin with %s' % (astring, template) -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing for the first few letters of a string
John Machin wrote: import re template = '^My name is alex' The ^ is redundant. Didn't the OP want to match only at the beginning of the string? m. -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing for the first few letters of a string
On Thu, 07 Aug 2008 23:08:10 +0200, magloca [EMAIL PROTECTED] wrote: John Machin wrote: import re template = '^My name is alex' The ^ is redundant. Didn't the OP want to match only at the beginning of the string? That's why it's redundant instead of incorrect. ;) re.match = match(pattern, string, flags=0) Try to apply the pattern at the *start* of the string, returning a match object, or None if no match was found. Emphasis mine. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list