Matching a constant string at beginning
Fred L. Drake, Jr.
fdrake at acm.org
Thu Dec 23 09:30:38 EST 1999
Hrvoje Niksic writes:
> Very cool!
And all thanks to that python-mode hacker, Barry Warsaw!
> It's long bothered me that you can't check whether a string starts
> with a phrase without either consing (s[:3] == 'foo') or resorting to
> regexps (re.match('^foo', s)).
What I've done occaisionally to avoid the magic number problem is
this:
s1 = some string...
s2 = "match this!"
if s1[:len(s2)] == s2:
# whatever...
It's a little too idiomatic for me to really like it, but makes it
easier to keep track of.
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
Corporation for National Research Initiatives
More information about the Python-list
mailing list