user5802211
user5802211

Reputation: 197

python how to split string with more than one character?

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

Answers (3)

sdvd
sdvd

Reputation: 443

Another way to solve it using re package

r = re.search('([0-9]*)([a-zA-Z]*)', test_string)
r.groups()

Upvotes: 2

Chris
Chris

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 numbers

Upvotes: 2

hilberts_drinking_problem
hilberts_drinking_problem

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

Related Questions