quick regex question

Steven Bethard steven.bethard at gmail.com
Thu Oct 28 19:14:19 CEST 2004


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) =)

>>> string1 = 'string with spaces'
>>> ''.join([s.title() for s in string1.split()])
'StringWithSpaces'

Clear, concise, and uses in the builtin str.title method, which appears to do
exactly what you want.

Steve




More information about the Python-list mailing list