Chr*_*ant 0 python string variables if-statement
我试图找出一种方法来查看字符串中的字符是否在另一个字符串之前得到并输出.说:
v="Hello There"
x=v[0]
if "Hello" in x:
print("V consists of '"'Hello'"'")
if "There" in x:
print("Hello comes before There)
if "There" in x:
print("V consists of '"'There'"'")
if "Hello" in x:
print("There comes before Hello")
Run Code Online (Sandbox Code Playgroud)
我想要得到的是"你好来到那里",虽然它在我输入时似乎不起作用.帮助将不胜感激.
之所以输出会指示Hello在之前出现是因为脚本是从上到下读取的,这只是对这一事实的利用.
如果其中任何一项没有任何意义,请随时在答案部分与我联系.
对于字符串's',s.find(substring)返回s开始的最低索引substring
if s.find('There') < s.find('Hello'):
print('There comes before Hello')
Run Code Online (Sandbox Code Playgroud)