查找字符串中第一个数字的索引 [英] Find the index of the first digit in a string

查看:36
本文介绍了查找字符串中第一个数字的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

这样的字符串

"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

这篇关于查找字符串中第一个数字的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆