[英]How can I retrieve characters from a string after finding them?
I would like to search some characters in a string and then save them in a variable but the way it is written in the string.我想在字符串中搜索一些字符,然后将它们保存在一个变量中,但它是在字符串中写入的方式。 For example:
例如:
text = "this is a Long text"
searchinput = "a long"
if searchinput.lower() in text.lower():
print(searchinput)
The output is, of course, "a long", but I want the output to be exactly as in the text variable, that is "a Long".输出当然是“a long”,但我希望输出与文本变量中的完全一样,即“a Long”。
In short: Check if a string is in a longer string and than retrieve the long string version.简而言之:检查字符串是否在更长的字符串中,然后检索长字符串版本。 How can I do that?
我怎样才能做到这一点?
You could try to get the index and then go from there.您可以尝试获取索引,然后从那里开始。 This is an example to give you an idea.
这是一个给你一个想法的例子。
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.