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()
There are only 10 kinds of people in this world: those who know binary and those who don’t.
There are 10 types of people in the world. Those who understand trinary, those who don’t, and those who mistake it for binary.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn 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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.