在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
。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:php怎么查找字符串中的某个字符