Get only numbers from string in python

后端 未结 8 1211
感动是毒
感动是毒 2020-12-06 18:37

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

8条回答
  •  失恋的感觉
    2020-12-06 18:51

    I think bdev TJ's answer

    price = int(filter(str.isdigit, just))
    

    will only work in Python2, for Python3 (3.7 is what I checked) use:

    price = int ( ''.join(filter(str.isdigit, just) ) )
    

    Obviously and as stated before, this approach will only yield an integer containing all the digits 0-9 in sequence from an input string, nothing more.

提交回复
热议问题