查找字符串中特定字符的索引

时间:2019-05-06 20:48:22

标签: python-3.x

如何找到给定字符串中出现字符的索引

我已经用Python 3编写了这段代码

def find(str,ch):
index=0
while index<len(str):
    if str[index]==ch:
        return index
    index=index+1

return -1

str=input("enter string")
ch=input(enter character")
print(find(str,ch))

如果我输入一个字符串apple,并且想要找到p的索引,我应该得到的输出为1和2。 但是我得到的输出仅为1

0 个答案:

没有答案