📅  最后修改于: 2023-12-03 15:34:12.732000             🧑  作者: Mango
如果你需要在Python中检查一个字符串是否在另一个字符串中出现,Python提供了几种方法来完成这个任务。以下是其中两种最常用的方法:
input_str = "Python is a popular programming language"
search_str = "programming"
if search_str in input_str:
print("Yes, the string 'programming' is found in the input string.")
else:
print("No, the string 'programming' is not found in the input string.")
该方法使用'in'关键词在输入字符串中查找搜索字符串。如果搜索字符串被找到,程序会输出" Yes, the string 'search_str' is found in the input string.",否则输出" No, the string 'search_str' is not found in the input string."。
import re
input_str = "Python is a popular programming language"
search_str = "programming"
if re.search(search_str, input_str):
print("Yes, the string 'programming' is found in the input string.")
else:
print("No, the string 'programming' is not found in the input string.")
该方法使用're'模块中的're.search()'函数在输入字符串中查找搜索字符串。如果搜索字符串被找到,程序会输出" Yes, the string 'search_str' is found in the input string.",否则输出" No, the string 'search_str' is not found in the input string."。
以上两种方法都可以轻松地在Python中检查字符串是否在输入中。