How to Split a String Between Numbers and Letters?

Problem Formulation: Given a string of letters and numbers. How to split the string into substrings of either letters or numbers by using the boundary between a letter and a number and vice versa.

Examples: Have a look at the following examples of what you want to accomplish.

'111A222B333C'     --->    ['111', 'A', '222', 'B', '333', 'C']
'Finxter42'        --->    ['Finxter', '42']
'Hello world'     --->    ['Hello', ' world']

Method 1: re.split()

The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b'].

# Method 1: re.split()
import re
s = '111A222B333C'
res = re.split('(\d+)', s)
# ['', '111', 'A', '222', 'B', '333', ' C']

The \d special character matches any digit between 0 and 9. By using the maximal number of digits as a delimiter, you split along the digit-word boundary. Note that you don’t consume the split character by wrapping it into a matching group using the parentheses (...). If you leave out the parentheses, it’ll consume the numbers and the result wouldn’t contain any consecutive numbers.

Method 2: re.findall()

The re.findall(pattern, string) method scans string from left to right, searching for all non-overlapping matches of the pattern. It returns a list of strings in the matching order when scanning the string from left to right.

# Method 2: re.findall()
import re
s = '111A222B333C'

res = re.findall('(\d+|[A-Za-z]+)', s)

# ['111', 'A', '222', 'B', '333', 'C']

Method 3: itertools.groupby()

# Method 3: itertools.groupby()
from itertools import groupby
s = '111A222B333C'

res = [''.join(g) for _, g in groupby(s, str.isalpha)]
# ['111', 'A', '222', 'B', '333', 'C']
  • The itertools.groupby(iterable, key=None) function creates an iterator that returns tuples (key, group-iterator) grouped by each value of key. We use the str.isalpha() function as key function.
  • The str.isalpha() function returns True if the string consists only of alphabetic characters.

Related Video re.split()

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!