Last updated: Jun 23, 2022
Photo from Unsplash
re.split() method to split a string into text and number, e.g.
my_list = re.split(r'(\d+)', my_str). The
re.split() method will split the
string on the digits and will still include them in the list.
import re my_str = 'hello123' my_list = re.split(r'(\d+)', my_str) # 👇️ ['hello', '123', ''] print(my_list)
Notice that we got an empty string at the end because the last character in the string is a digit.
You can use the
filter() method to remove any empty strings from the list.
import re my_str = 'hello123' my_list = list(filter(None, re.split(r'(\d+)', my_str))) # 👇️ ['hello', '123'] print(my_list)
The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
If you pass
None for the function argument, all falsy elements of the iterable
The re.split method takes a pattern and a string and splits the string on each occurrence of the pattern.
The group's contents can still be retrieved after the match.
Even though we split the string on one or more digits, we still include the digits in the result.
\d character matches the digits
[0-9] (and many other digit characters).
+ matches the preceding regular expression 1 or more times.
This approach also works if your string starts with digits and ends in characters.
import re my_str = '123hello' my_list = list(filter(None, re.split(r'(\d+)', my_str))) # 👇️ ['123', 'hello'] print(my_list)
If we didn't use the
filter() function, we'd have an empty string element at
the start of the list.
Note that the
filter function returns a filter object (not a list). If you
need to convert the
filter object to a list, pass it to the