Fix Python – Split a string at uppercase letters


Asked By – Federico A. Ramponi

What is the pythonic way to split a string before the occurrences of a given set of characters?

For example, I want to split
at any occurrence of an uppercase letter (possibly except the first), and obtain
['The', 'Long', 'And', 'Winding', 'Road'].

Edit: It should also split single occurrences, i.e.
from 'ABC' I’d like to obtain
['A', 'B', 'C'].

Now we will see solution for issue: Split a string at uppercase letters


Unfortunately it’s not possible to split on a zero-width match in Python. But you can use re.findall instead:

>>> import re
>>> re.findall('[A-Z][^A-Z]*', 'TheLongAndWindingRoad')
['The', 'Long', 'And', 'Winding', 'Road']
>>> re.findall('[A-Z][^A-Z]*', 'ABC')
['A', 'B', 'C']

This question is answered By – Mark Byers

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0