str_1='wo shi yi zhi da da niu ' char_1='i' nPos=str_1.index(char_1) print(nPos)
运行结果:7
========是使用find==========
str_1='wo shi yi zhi da da niu ' char_1='i' nPos=str_1.find(char_1) print(nPos)
结果:5
========如何查找所有‘i’在字符串中位置呢?===========
#开挂模式 str_1='wo shi yi zhi da da niu ' char_1=str(input('Please input the Char you want:')) count=0 str_list=list(str_1) for each_char in str_list:
count+=1
if each_char==char_1:
print(each_char,count-1)
运行结果:
Please input the Char you want:i
i 0
i 1
i 2
i 3
本文介绍了如何在Python中使用index和find方法查找字符串中特定字符首次出现的位置,并提供了一种方法来找出字符串中所有指定字符的位置。
1592

被折叠的 条评论
为什么被折叠?



