本文介绍了查找字符串中第一个数字的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像
这样的字符串"xdtwkeltjwlkejt7wthwk89lk"如何获取字符串中第一个数字的索引?
解决方案使用 re.search():
>>>进口重新>>>s1 = "thishasadigit4here";>>>m = re.search(r"\d", s1)>>>如果米:...打印(在位置找到数字",m.start())... 别的:...打印(该字符串中没有数字")...在第 13 位找到的数字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
更多推荐
查找字符串中第一个数字的索引
发布评论