如何在 python 字符串中找到子字符串的第一次出现? [英] How can I find the first occurrence of a sub-string in a python string?

查看:95
本文介绍了如何在 python 字符串中找到子字符串的第一次出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以如果我的字符串是这个家伙是个很酷的家伙".
我想找到'dude'的第一个索引:

mystring.findfirstindex('dude') # 应该返回 4

这个的python命令是什么?
谢谢.

解决方案

find()

<预><代码>>>>s = "这个家伙是个很酷的家伙">>>s.find('老兄')4

So if my string is "the dude is a cool dude".
I'd like to find the first index of 'dude':

mystring.findfirstindex('dude') # should return 4

What is the python command for this?
Thanks.

解决方案

find()

>>> s = "the dude is a cool dude"
>>> s.find('dude')
4

这篇关于如何在 python 字符串中找到子字符串的第一次出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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