I have a string that stores a number and a unit for example
x= '$120'
y = ' 90 Degrees F'
banana = '200 kgm'
orange = '300 gm'
total_weight = banana + orange/1000
and for example I want to add the weights
total_weight = 200 + 300/1000
Thanks!
I’m trying to extract the numbers only to do some operations with these… any idea of what the simplest way to do this? I’m only dealing with these two formats i.e. digits are at the begining or at the end of the string…
The simplest way to extract a number from a string is to use regular expressions and
findall.It might be that you need something more complex, but this is a good first step.
Note that you’ll still have to call
inton the result to get a proper numeric type (rather than another string):