温馨提示×

python怎么查找字符串中某个字符

小亿
85
2024-03-02 13:11:20
栏目: 编程语言

在Python中,可以使用find()方法来查找字符串中某个字符的索引位置。示例如下:

s = "Hello, World!"
char = "o"
index = s.find(char)
if index != -1:
    print(f"Found '{char}' at index {index}")
else:
    print(f"'{char}' not found")

这段代码将在字符串s中查找字符o,并输出找到的索引位置。如果字符不存在,则输出'o' not found

0