查找字符串中第一个数字的索引

发布于 2021-01-29 15:15:07

我有一个像

"xdtwkeltjwlkejt7wthwk89lk"

如何获取字符串中第一位的索引?

关注者
0
被浏览
142
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    用途re.search()

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看