[英]How to get the index of remaining string index after regexp in Python?
[英]Python: How to get the Index of a string
我如何編寫一個不使用索引方法又返回一個字符串中單個字符的索引的函數,也就是使用string.index('some random character')
?
處理字符串中多次出現char的另一個變體。
def idx(string, char):
for key, x in enumerate(string):
if x == char:
print key
index()與find()相同,除了找不到字符串時的錯誤。
這是學校的任務嗎?
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.