I want to get only the numbers from a string. For example I have something like this
just=\'Standard Price:20000\'
And I only want it to pr
you can use regex:
import re
just = 'Standard Price:20000'
price = re.findall("\d+", just)[0]
OR
price = just.split(":")[1]
If you want to keep it simpler avoiding regex, you can also try Python's built-in function filter
with str.isdigit
function to get the string of digits and convert the returned string to integer. This will not work for float as the decimal character is filtered out by str.isdigit
.
Python Built-in Functions Filter
Python Built-in Types str.isdigit
Considering the same code from the question:
>>> just='Standard Price:20000'
>>> price = int(filter(str.isdigit, just))
>>> price
20000
>>> type(price)
<type 'int'>
>>>