如下所示:
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=0str_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:ii 0i 1i 2i 3
以上这篇Python 查找字符在字符串中的位置实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- python实现在字符串中查找子字符串的方法
- python 查找字符串是否存在实例详解
- Python中的字符串查找操作方法总结
- python实现从字符串中找出字符1的位置以及个数的方法
- Python字符串中查找子串小技巧
- Python 实现字符串中指定位置插入一个字符