Split a string into text and integer
Question
I have a string of the form
" This year is 2014"
I would like to split the string into two parts
part1="This year is"
part2="2014"
Solution
Split on whitespace followed by a digit, like this:
>>> part1, part2 = re.split(r'\s(?=\d)', " This year is 2014", 1)
>>> part1, part2
(' This year is', '2014')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow