How can I add spaces where ever I have capital letters?

MRAB google at mrabarnett.plus.com
Fri May 9 09:28:10 EDT 2008


On May 9, 3:04 am, "Eric Wertman" <ewert... at gmail.com> wrote:
> Something like this.  I'm sure there are other ways to do it.
>
> import re
>
> def addspace(m) :
>         return ' ' + m.group(0)
>
> strng = "ModeCommand"
>
> newstr =  re.sub('[A-Z]',addspace,strng)
>
Alternatively:

newstr = re.sub('([A-Z])',r' \1',strng)

> print newstr.strip()
>
> On Thu, May 8, 2008 at 9:12 PM, John Schroeder <jschr... at gmail.com> wrote:
> > I have a string (which I got from the names of my classes) and I would like
> > to print out my CamelCase classes as titles.
>
> > I would like it to do this:
>
> >>>> my_class_name = "ModeCommand"
> > ## Do some magic here
> >>>> my_class_name
> > 'Mode Command'
>
> > Anyone know any easy way to do this?  Thanks.
>



More information about the Python-list mailing list