quick regex question

Peter Otten __peter__ at web.de
Fri Oct 29 16:44:21 EDT 2004


Oliver Fromme wrote:

>  > > >>> string1 = 'string with spaces'
>  > > >>> ''.join([s.title() for s in string1.split()])
>  > > 'StringWithSpaces'
>  > 
>  > Or, without the list comprehension:
>  > 
>  > >>> "".join("string with spaces".title().split())
>  > 'StringWithSpaces'
>  > 
>  > > Clear, concise, and uses in the builtin str.title method, which
>  > > appears to do exactly what you want.
> 
> Uhm, I think the following is more efficient:
> 
>>>> "string with spaces".title().replace(" ", "")
> 'StringWithSpaces'
> 
> It doesn't split into a temporary list of strings which
> are then joined together again.  Also, I believe it's more
> readable than the split/join approach.

You win :-)

Peter
 




More information about the Python-list mailing list