473,602 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "ModeComman d"

newstr = re.sub('[A-Z]',addspace,strn g)

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_nam e = "ModeComman d"
## Do some magic here
>>>my_class_nam e
'Mode Command'

Anyone know any easy way to do this? Thanks.

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
2 3361
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_na me = "ModeComman d"
## Do some magic here
>>my_class_na me
'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 = "ModeComman d"

newstr = *re.sub('[A-Z]',addspace,strn g)

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 = "ModeComman d"

newstr = re.sub('[A-Z]',addspace,strn g)
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_na me = "ModeComman d"
## Do some magic here
>>my_class_na me
'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
10976
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 expression or short function that can do this?
4
10766
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
29762
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 you will see a sentence and then the tag.<span style='mso-spacerun: yes'> </span>Well, this next sentence will occur two spaces later because there are two spaces in the span. How to do this without using MS's style? Is there some way to...
2
6585
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 Ebrahim Khademi
1
3122
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 CharacterCasing metod for a COMBOBOX...
135
7432
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 which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the situation behind it, and which is proper.
2
3988
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 one word but if there is spaces or capital lettersor commas it won't work I used getline but without any effect.here is the code. #include <iostream> #include <string> using namespace std; int main () {
5
16976
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
2248
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 person typing. The problem is, is that when it is run in the python shell, it puts spaces in between the letters of my message. Please submit a version of this that will take the spaces out of the Message being written by the ghost. (And yes, I am...
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
7921
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8405
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8270
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1511
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.