I am creating a metric measurement converter. The user is expected to enter in an expression such as 125km
(a number followed by a unit abbreviation). For conversion, the numerical value must be split from the abbreviation, producing a result such as [125, 'km']
. I have done this with a regular expression, re.split
, however it produces unwanted item in the resulting list:
import re
s = '125km'
print(re.split('(\d+)', s))
Output:
['', '125', 'km']
I do not need nor want the beginning ''
. How can I simply separate the numerical part of the string from the alphabetical part to produce a list using a regular expression?