Testing for the first few letters of a string

John Machin sjmachin at lexicon.net
Thu Aug 7 16:54:42 EDT 2008


On Aug 8, 1:53 am, Edwin.Mad... at VerizonWireless.com 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)
>



More information about the Python-list mailing list