如何在python中查找子字串的最后一個字符的索引例如:s =“hello”
s.find(“he”)中最后一個字符'e'的索引
uj5u.com熱心網友回復:
您可以通過以下方式簡單地做到這一點 -
string = "hello"
subStr = "he"
if (string.find(subStr) != -1):
print(string[string.find(subStr) len(subStr)-1])
else:
print("Substring not found")
uj5u.com熱心網友回復:
if substr in s:
idx = s.find(substr) len(substr) - 1
print(s[idx])
在你的情況下 substr = 'he'
s.find() 回傳子字串第一個符號的索引。您需要添加 len(substr) - 1 以獲取子字串的最后一個符號的索引
uj5u.com熱心網友回復:
這是應該做的
def get_index(s,substring):
start = s.find(substring)
if(start != -1):
return start len(substring) - 1
else:
print("substring not found")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412116.html
標籤:
下一篇:基于特定組連接列
