《python语言程序设计》2018版第8章第2题检测子串,你可以用str类中的find方法检测一个字符串
我先用in来做一次
def find_text(text_input1, text_input2):a = str(text_input1)b = str(text_input2)if b in a:print(f"The {b} is in {a} ")else:print(f"The {b} is not in {a} ")text_n1 = "Welcome to shenyang"
text_n2 = "to"find_text(text_n1, text_n2)
使用find
def find_text(text_input1, text_input2):a = str(text_input1)b = str(text_input2)if a.find(b) != -1:print(f"The {b} is in {a} ")else:print(f"The {b} is not in {a} ")text_n1 = "Welcome to shenyang"
text_n2 = "to"find_text(text_n1, text_n2)