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

John Schroeder jschroed at gmail.com
Thu May 8 22:50:20 EDT 2008


I do like one-liners and do not like regexps!  That code puts mine to
shame...  Thanks for the tip.  I'll use that one instead.

On Thu, May 8, 2008 at 7:40 PM, Dan Bishop <danb_83 at yahoo.com> wrote:

> > 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.
>
> On May 8, 9:04 pm, "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)
> >
> > print newstr.strip()
>
> Yes, there are other ways to do it.  If, for example, you like one-
> liners but not regexps:
>
> def add_spaces(text):
>    return text[:1] + ''.join((' ' + char if char.isupper() else char)
> for char in text[1:])
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080508/4fd98141/attachment-0001.html>


More information about the Python-list mailing list