Search code examples
pythonstringsplit

How to split strings into text and number?


I'd like to split strings like these

'foofo21'
'bar432'
'foobar12345'

into

['foofo', '21']
['bar', '432']
['foobar', '12345']

Does somebody know an easy and simple way to do this in python?


Solution

  • I would approach this by using re.match in the following way:

    import re
    match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I)
    if match:
        items = match.groups()
    print(items)
    >> ("foofo", "21")