Testing for the first few letters of a string

Edwin.Madari at VerizonWireless.com Edwin.Madari at VerizonWireless.com
Thu Aug 7 11:53:53 EDT 2008


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: python-list-bounces+edwin.madari=verizonwireless.com at python.org
[mailto:python-list-bounces+edwin.madari=verizonwireless.com at python.org]
On Behalf Of Alexnb
Sent: Thursday, August 07, 2008 11:40 AM
To: python-list at 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.





More information about the Python-list mailing list