Find the index of the first digit in a string

前端 未结 14 835
清酒与你
清酒与你 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:32

    In Python 3.8+ you can use re.search to look for the first \d (for digit) character class like this:

    import re
    
    my_string = "xdtwkeltjwlkejt7wthwk89lk"
    
    if first_digit := re.search(r"\d", my_string):
        print(first_digit.start())
    

提交回复
热议问题