By user http://code.activestate.com/recipes/users/2629617/ in comment on http://code.activestate.com/recipes/440698/ but modified slightly.
Splits any string on upper case characters.
Ex.
>>> print split_uppercase("thisIsIt and SoIsThis")
this Is It and So Is This
note the two spaces after 'and'
1 2 3 4 5 6 7 8 9 10 11 12 13 | def split_uppercase(str):
x = ''
i = 0
for c in str:
print c, str[i-1]
if i == 0:
x += c
elif c.isupper() and not str[i-1].isupper():
x += ' %s' % c
else:
x += c
i += 1
return x.strip()
|
An alternative is to use the re module
Having the same behavior.
Sorry, but people might use this as a learning tool, so I need to point out a few mistakes:
str
is the string type and should not be used as a variable name.for i, c in enumerate(str):
My version would be:
You can get rid of the extra space by using
l = c.islower()
instead ofl = not c.isupper()
.Also the regular expression given by r. is wrong. It inserts a space before every uppercase character, even the ones following an uppercase character. A better one would be
re.sub(r'([^A-Z])([A-Z])', r'\1 \2', value)
. Also keep in mind that the regex does not take accented characters into account.LOL, first of all - what horrible way of naming variable! of any name, you chose to use "str" ????? This Recipe sucks