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
相关推荐
一只码畜 2020-01-28
xiaoseyihe 2020-08-11
pythonxuexi 2020-07-30
leoaran 2020-07-21
littie 2020-07-19
zcabcd 2020-07-18
yjsflxiang 2020-07-04
liusarazhang 2020-06-28
银角大王 2020-06-25
leoaran 2020-06-22
Lexan 2020-06-15
liusarazhang 2020-06-14
89411051 2020-06-14
chinademon 2020-06-11
leoaran 2020-06-06
tengyuan 2020-06-03
shengnanonly 2020-06-03
joyjoy0 2020-06-02