Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How can I add spaces where ever I have capital letters?

Reply
Thread Tools

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

 
 
Eric Wertman
Guest
Posts: n/a
 
      05-09-2008
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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      05-09-2008
> On Thu, May 8, 2008 at 9:12 PM, John Schroeder <(E-Mail Removed)> 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" <(E-Mail Removed)> 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:])
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      05-09-2008
On May 9, 3:04 am, "Eric Wertman" <(E-Mail Removed)> 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 <(E-Mail Removed)> 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.

>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[CSS] how can I show spaces as spaces? Tomasz Chmielewski HTML 21 09-10-2009 06:43 PM
how can I show spaces as spaces, part 2 Tomasz Chmielewski HTML 14 09-10-2009 03:54 PM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM



Advertisments