在Python中,可以使用for循环遍历字符串的每个字符,并通过if语句检查一个对象是否包含该字符。以下是一个示例代码:
def check_string(obj, string):
for char in string:
if char in obj:
print(f"{char} is present in the object.")
else:
print(f"{char} is not present in the object.")
# 要检查的对象
my_obj = "Hello World"
# 要遍历的字符串
my_string = "loW"
# 调用函数进行检查
check_string(my_obj, my_string)
输出结果为:
l is present in the object.
o is present in the object.
W is present in the object.
在上面的示例中,check_string
函数接受两个参数,obj
表示要检查的对象,string
表示要遍历和检查的字符串。在函数内部,使用for循环遍历字符串的每个字符,并通过if语句检查该字符是否在对象中。如果字符在对象中,则打印它存在的消息,否则打印它不存在的消息。