[英]Python: How to get the Index of a string
我如何编写一个不使用索引方法又返回一个字符串中单个字符的索引的函数,也就是使用string.index('some random character')
?
Another variant that works with multiple occurrences of char in string. 处理字符串中多次出现char的另一个变体。
def idx(string, char):
for key, x in enumerate(string):
if x == char:
print key
index()与find()相同,除了找不到字符串时的错误。
it's a school task? 这是学校的任务吗?
def ind(the_string, the_char):
i = 0
for a_char in the_string:
if a_char == the_char: return i
i += 1
return -1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.