473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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 <js******@gmail.comwrote:
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
Jun 27 '08 #1
2 3350
On Thu, May 8, 2008 at 9:12 PM, John Schroeder <jschr...@gmail.comwrote:
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...@gmail.comwrote:
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:])
Jun 27 '08 #2
On May 9, 3:04 am, "Eric Wertman" <ewert...@gmail.comwrote:
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...@gmail.comwrote:
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.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
Hi, I am trying to insert a space before all capital letters in a string except the first occurrence. For example: FirstSecondThird would become First Second Third Is there a regular...
4
by: Carlos Marangon | last post by:
Hello! People go to sign my guestbook and wrote all text in capital letters. Did you know any script that shows an window alert when one types the second capital letter? Best regards,
15
by: Randall Parker | last post by:
I've noticed when exporting from Microsoft Word XP into an HTML file that Word uses a span style of mso-spacerun: yes. This has the effect of making there be about 2 spaces between sentences. So...
2
by: Ebi | last post by:
Hi I'm writing a project in C++ borland 5; and I need a function that get a string or char* variable and convert all of the small letters to the capital. please help me. -- Sincerely Yours...
1
by: thomaz | last post by:
How I can place in capital letters the text of a COMBOBOX? For TEXTBOX I use: textBox1.CharacterCasing = CharacterCasing.Upper;... How to make the same for a COMBOBOX? There isnt a...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
2
by: inferi9 | last post by:
hi, I am writng a program that takes a string and it must output if it is a palindrome, A palindrome is a string that is spelled and read the same way forward and backward, it workes fine with...
5
Presto731
by: Presto731 | last post by:
HOW do i make the letters in various fields on a form always be capital no matter if they used capital letters or not when typing?
11
tharden3
by: tharden3 | last post by:
Below I am disclosing a script I wrote. I called it GhostWriter. It is supposed to write a message you type in as if a ghost were writing it for you. It even has some very basic AI to simulate a real...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.