473,699 Members | 2,383 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 3366
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
10994
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
10768
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
29774
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
6592
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
3128
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
7468
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
3995
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
16984
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
2253
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
8697
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
8622
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
9184
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
9045
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...
1
8930
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7767
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2013
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.