[ACCEPTED]-Splitting a string by capital letters-python

Accepted answer
Score: 21

I think there is a far easier way to do 12 what you're trying to do. Use regular expressions. For 11 instance:

>>> [a for a in re.split(r'([A-Z][a-z]*)', 'MgSO4') if a]
['Mg', u'S', u'O', u'4']

If you want the number attached 10 to the right element, just add a digit specifier 9 in the regex:

>>> [a for a in re.split(r'([A-Z][a-z]*\d*)', txt) if a]
[u'Mg', u'S', u'O4']

You don't really want to "put 8 each part in its own variable". That doesn't 7 make sense in general, because you don't 6 know how many parts there are, so you can't 5 know how many variables to create ahead 4 of time. Instead, you want to make a list, like 3 in the example above. Then you can iterate 2 over this list and do what you need to do 1 with each piece.

Score: 11

You can use re.split to perform complex 1 splitting on strings.

import re

def split_upper(s):
    return filter(None, re.split("([A-Z][^A-Z]*)", s))

>>> split_upper("fooBarBaz")
['foo', 'Bar', 'Baz']
>>> split_upper("fooBarBazBB")
['foo', 'Bar', 'Baz', 'B', 'B']
>>> split_upper("fooBarBazBB4")
['foo', 'Bar', 'Baz', 'B', 'B4']

More Related questions