Find the index of the first digit in a string

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

    instr = 'nkfnkjbvhbef0njhb h2konoon8ll'
    numidx = next((i for i, s in enumerate(instr) if s.isdigit()), None)
    print numidx
    

    output:

    12
    

    numidx will be the index of the first occurrence of a digit in instr. If there are no digits in instr, numidx will be None.

    I didn't see this solution here, and thought it should be.

提交回复
热议问题