Find the index of the first digit in a string

前端 未结 14 875
清酒与你
清酒与你 2020-12-28 12:29

I have a string like

\"xdtwkeltjwlkejt7wthwk89lk\"

how can I get the index of the first digit in the string?

14条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 12:51

    Use re.search():

    >>> import re
    >>> s1 = "thishasadigit4here"
    >>> m = re.search(r"\d", s1)
    >>> if m:
    ...     print("Digit found at position", m.start())
    ... else:
    ...     print("No digit in that string")
    ... 
    Digit found at position 13
    

提交回复
热议问题