Get only numbers from string in python

后端 未结 8 1221
感动是毒
感动是毒 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:52

    You could use RegEx

    >>> import re
    >>> just='Standard Price:20000'
    >>> re.search(r'\d+',just).group()
    '20000'
    

    Ref: \d matches digits from 0 to 9

    Note: Your error

    just[0] evaluates to S as it is the 0th character. Thus S[1:] returns an empty string that is '', because the string is of length 1 and there are no other characters after length 1

提交回复
热议问题