慕田峪9158850
2023-09-05 15:14:29
我想搜索字符串中的一些字符,然后將它們保存在變量中,但以字符串中的寫(xiě)入方式保存。例如:text = "this is a Long text"searchinput = "a long"if searchinput.lower() in text.lower(): print(searchinput)輸出當(dāng)然是“a long”,但我希望輸出與文本變量中的輸出完全相同,即“a Long”。簡(jiǎn)而言之:檢查字符串是否在較長(zhǎng)字符串中,然后檢索長(zhǎng)字符串版本。我怎樣才能做到這一點(diǎn)?
2 回答

烙印99
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
您可以嘗試獲取索引,然后從那里開(kāi)始。這是一個(gè)給你一個(gè)想法的例子。
text = "this is a Long text"
check = "a long"
try:
i = text.lower().index(check.lower())
print(text[i:i+len(check)])
except:
print('Not Found')
添加回答
舉報(bào)
0/150
提交
取消