quick regex question

Peter Otten __peter__ at web.de
Thu Oct 28 14:23:23 EDT 2004


Steven Bethard wrote:

> Matt Price <matt.price <at> utoronto.ca> writes:
>> 
>> this is surely trivial, but can'f figure it out.  I wnat to replace:
>> 
>> 'string with spaces'
>> with
>> 'StringWithSpaces'
> 
> 
> I know you asked for a regex solution, but I thought I'd drop this
> suggestion anyway (not everything really merits a regex) =)

I second that :-)
 
>>>> 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.

Peter






More information about the Python-list mailing list