>> import re >>> s1-6ren">
gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:40:49 30 4
gpt4 key购买 nike

我有一个类似

的字符串
"xdtwkeltjwlkejt7wthwk89lk"

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

最佳答案

使用 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

关于python - 查找字符串中第一个数字的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4510709/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com