Matching a constant string at beginning

Barry A. Warsaw bwarsaw at cnri.reston.va.us
Thu Dec 23 09:25:05 EST 1999


>>>>> "I" == ISO  <pinard at iro.umontreal.ca> writes:

    I> is rather tedious.  Of course, I could write a very small
    I> function to match a constant string at the beginning of
    I> another, but there just must be some idiom for doing this.

In the current JPython betas, and in the next version of CPython
(development version avalable via CVS), string objects have methods.
Two new methods `startswith' and `endswith' have been added, which
will fit the bill perfectly:

-------------------- snip snip --------------------
Python 1.5.2+ (#8, Dec 22 1999, 17:27:05)  [GCC 2.8.1] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> s = "Warsaw ain't just the capital of Poland"
>>> s.startswith('Boston')
0
>>> s.startswith('Warsaw')
1
-------------------- snip snip --------------------

String methods will be one of those new features that'll change your
life.  :)  Wait'll you try s.join().

-------------------- snip snip --------------------
>>> ' & '.join(['Guido', 'Tim', 'Gordon', 'David', 'Biff', 'etc.'])
'Guido & Tim & Gordon & David & Biff & etc.'
>>> 
-------------------- snip snip --------------------

-Barry




More information about the Python-list mailing list