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

Discussion in 'Python' started by Eric Wertman, May 9, 2008.

  1. Eric Wertman

    Eric Wertman Guest

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



    On Thu, May 8, 2008 at 9:12 PM, John Schroeder <> 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.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Eric Wertman, May 9, 2008
    #1
    1. Advertising

  2. Eric Wertman

    Dan Bishop Guest

    > On Thu, May 8, 2008 at 9:12 PM, John Schroeder <> 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" <> 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:])
     
    Dan Bishop, May 9, 2008
    #2
    1. Advertising

  3. Eric Wertman

    MRAB Guest

    On May 9, 3:04 am, "Eric Wertman" <> 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 <> 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.

    >
     
    MRAB, May 9, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ebi
    Replies:
    2
    Views:
    12,635
    puzzlecracker
    Feb 5, 2005
  2. Miguel Orrego

    Capital Letters

    Miguel Orrego, Aug 19, 2003, in forum: ASP General
    Replies:
    6
    Views:
    241
    Dave Anderson
    Aug 20, 2003
  3. Ralph Shnelvar

    Splitting on capital letters

    Ralph Shnelvar, Dec 8, 2010, in forum: Ruby
    Replies:
    12
    Views:
    349
    Robert Klemme
    Dec 9, 2010
  4. Keith Bowes

    Avoid All Capital Letters in Guestbook

    Keith Bowes, Jan 6, 2004, in forum: Javascript
    Replies:
    4
    Views:
    833
    Thomas 'PointedEars' Lahn
    Jan 15, 2004
  5. Venugopal
    Replies:
    11
    Views:
    1,534
    Tassilo v. Parseval
    Nov 5, 2003
Loading...

Share This Page