Welcome, guest | Sign In | My Account | Store | Cart

This function accepts a string and returns a string with whitespace(one space) inserted between words with leading capitalized letters.

Python, 15 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import re
def split_on_caps(str):
    
    rs = re.findall('[A-Z][^A-Z]*',str)
    fs = ""
    for word in rs:
        fs += " "+word
    
    return fs
#test
if __name__ == "__main__":
    print split_on_caps("DonkeyIsAGreatBeastYIP")


---> Donkey Is A Great Beast Y I P 

usefull for getting human readable names of funtion names. ie: thisFunctionDoesSomething() --> this Function Does Something()

Another way to do this without using RE is to use the following in place of the re.findall():

search = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' sentence = 'The Quick Brown Fox Bites The Lazy Dog' "string recieved" i=0 for letter in sentence: if letter in search: print 'letter ',letter,' found at pos:',i i+=1

Issues: Doesn't return any text before the first uppercase letter. simple to fix

Helpful info, #python on freenode. http://www.amk.ca/python/howto/regex/ Thanks to RexFi for the re help.

6 comments

Matteo Dell'Amico 18 years, 6 months ago  # | flag

Doesn't work as specified. This won't work as advertised, for instance, with 'thisFunctionDoesSomething'. Since 'this' won't match the [A-Z][^A-Z] regexp, it won't show up in re.findall.

This function will add a space in each uppercase-lowercase boundary.

def split_uppercase(string):
    return re.sub(r'([a-z])([A-Z])', r'\1 \2', string)
Walter Brunswick 18 years, 6 months ago  # | flag

Completely Useless. thisFunctionDoesSomething() is a perfectly-readable function name.

Walter Brunswick 18 years, 6 months ago  # | flag

Regex Correction. First off, I'd like the say that this recipe is a complete waste of time. Please don't ask me why I'm spending time fixing it.

Secondly, there's an error in your regex.

>>> split_uppercase('thisIsAFunctionName');
'this Is AFunction Name'

That can be fixed with the following...

def split_uppercase(s):
    return re.sub(r'([a-z]*)([A-Z])',r'\1 \2',s)



>>> split_uppercase('thisIsAFunctionName')
'this Is A Function Name'
luis gonzalez 18 years, 6 months ago  # | flag

Simple. No regex. def split_uppercase(string): x='' for i in string: if i.isupper(): x+=' %s' %i else: x+=i return x.strip()

>>> split_uppercase('HolaQueTal')
'Hola Que Tal'
Sam Denton 13 years, 7 months ago  # | flag

I like luis gonzalez's idea, but appending to a string is expensive in Python, as the entire string has to be copied. Appending to an array is cheaper, and typically saves enough time that you don't mind doing the join. I also spiced things up a bit with some extra options.

def split_name(name, upper_first=True, upper_after_underscore=True):
    upper_first = True
    title = []
    for ch in name:
        if ch.isupper() and not upper_first:
            title.append(' ')
        if upper_first:
            ch = ch.upper()
            upper_first = False
        if ch == '_':
            ch = ' '
            upper_first = upper_after_underscore
        title.append(ch)
    return ''.join(title)

Use it like this:

>>> split_name('thisIsATest')
'This Is A Test'
>>> split_name('ThisIsATest')
'This Is A Test'
>>> split_name('this_is_a_test')
'This Is A Test'
John Vandenberg 8 years, 2 months ago  # | flag

Any of these solutions using re, string.uppercase, str.isupper, etc. only works for ASCII uppercase, or at best is locale dependant.

Once approach that works for non-English text, and is locale independent, is to use [:upper:] and regex.UNICODE with the regex module.

Created by Graeme Glass on Wed, 12 Oct 2005 (PSF)
Python recipes (4591)
Graeme Glass's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks