Python中判断字符串中是否包含另一个字符串

第一种方法:使用in

def demo():
    str1="b"
    str2="abc"
    if str1 in str2:
        print("存在")
    else:
        print("不存在")    

demo()

执行上述代码,其输出结果为:

存在

第二种方法:使用find(推荐使用)

def demo():
    str1="a"
    str2="abc"
    if str2.find(str1)>=0:
        #包含的话,返回第一次出现的位置,位置下标是从0开始,没有的话为负数
        print("包含")
    else:
        print("不包含")    

demo()

执行上述代码,其输出结果为:

包含

第三种方法:使用find

Guess you like

Origin blog.csdn.net/y_bccl27/article/details/121487055