Python判断字符串是否包含特定字符的方法就是str.contains。它是Python中的一个字符串方法,用于检查字符串中是否包含特定的字符串,如果字符串中包含特定字符串,则返回True,如果不包含,则返回False。
str.contains的使用方法
str.contains的使用方法很简单,只需要在字符串后面加上.contains('要查找的字符串')即可,如果要查找的字符串在原字符串中,则返回True,如果不在,则返回False。例如:
str1 = 'Python is a great language' if str1.contains('Python'): print('Yes') else: print('No') # 返回Yes
str.contains()方法还可以指定开始位置和结束位置,可以更精确地查找字符串,例如:
str1 = 'Python is a great language' if str1.contains('Python', 0, 10): print('Yes') else: print('No') # 返回Yes
str.contains()方法还可以指定查找的字符串的大小写,例如:
str1 = 'Python is a great language' if str1.contains('python', 0, 10, ignore_case=True): print('Yes') else: print('No') # 返回Yes
:str.contains()方法可以用来检查字符串中是否包含特定字符串,如果字符串中包含特定字符串,则返回True,如果不包含,则返回False。可以指定开始位置和结束位置,也可以指定查找的字符串的大小写。