查找字符串中第一个数字的索引 [英] Find the index of the first digit in a string
本文介绍了查找字符串中第一个数字的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像
这样的字符串"xdtwkeltjwlkejt7wthwk89lk"
如何获取字符串中第一个数字的索引?
解决方案
使用 re.search()
:
I have a string like
"xdtwkeltjwlkejt7wthwk89lk"
how can I get the index of the first digit in the string?
解决方案
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
这篇关于查找字符串中第一个数字的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文