Reputation: 197
I would like to split a string as below
1234ABC
into 123
and ABC
2B
into 2
and B
10E
into 10
and E
I found split
function does not work because there is no delimiter
Upvotes: 0
Views: 1102
Reputation: 443
Another way to solve it using re package
r = re.search('([0-9]*)([a-zA-Z]*)', test_string)
r.groups()
Upvotes: 2
Reputation: 22953
This can quite easily be accomplished using the re
module:
>>> import re
>>>
>>> re.findall('[a-zA-Z]+|[0-9]+', '1234ABC')
['1234', 'ABC']
>>> re.findall('[a-zA-Z]+|[0-9]+', '2B')
['2', 'B']
>>> re.findall('[a-zA-Z]+|[0-9]+', '10E')
['10', 'E']
>>> # addtionall test case
...
>>> re.findall('[a-zA-Z]+|[0-9]+', 'abcd1234efgh5678')
['abcd', '1234', 'efgh', '5678']
>>>
The regex use is very simple. Here is quick walk through:
[a-zA-Z]+
: Match one or more alphabetic characters lower case or upper|
or...[0-9]+
: One or more whole numbersUpvotes: 2
Reputation: 11602
You can use itertools.groupby
with boolean isdigit
function.
from itertools import groupby
test1 = '123ABC'
test2 = '2B'
test3 = '10E'
def custom_split(s):
return [''.join(gp) for _, gp in groupby(s, lambda char: char.isdigit())]
for t in [test1, test2, test3]:
print(custom_split(t))
# ['123', 'ABC']
# ['2', 'B']
# ['10', 'E']
Upvotes: 5