new string method in 2.5 (partition)

John Salerno johnjsal at NOSPAMgmail.com
Tue Sep 19 16:35:45 EDT 2006


Larry Bates wrote:
> John Salerno wrote:
>> Bruno Desthuilliers wrote:
>>
>>> Err... is it me being dumb, or is it a perfect use case for str.split ?
>> Hmm, I suppose you could get nearly the same functionality as using
>> split(':', 1), but with partition you also get the separator returned as
>> well.
>>
>>> There are IMVHO  much exciting new features in 2.5 (enhanced
>>> generators, try/except/finally, ternary operator, with: statement etc...)
>> I definitely agree, but I figure everyone knows about those already.
>> There are also the startswith() and endswith() string methods that are
>> new and seem neat as well.
> 
> FYI- .startswith() and .endswith() string methods aren't new in 2.5.
> They have been around since at least 2.3.
> 
> Larry Bates

Oops, just a slight change in their functionality:

The startswith() and endswith() methods of string types now accept 
tuples of strings to check for.

def is_image_file (filename):
     return filename.endswith(('.gif', '.jpg', '.tiff'))

(Implemented by Georg Brandl following a suggestion by Tom Lynn.)



More information about the Python-list mailing list