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()
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) print(res) # ['', '111', 'A', '222', 'B', '333', ' C']
\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()
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) print(res) # ['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)] print(res) # ['111', 'A', '222', 'B', '333', 'C']
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.
Trueif 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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.