好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python查找字符在字符串中的位置实例

下面为大家分享一篇Python 查找字符在字符串中的位置实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

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查找目录下指定扩展名的文件实例

以上就是Python 查找字符在字符串中的位置实例的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python查找字符在字符串中的位置实例的详细内容...

  阅读:58次