编程语言
首页 > 编程语言> > 【Python 字符串】-判断一个字符串是否包含某个指定的字符串

【Python 字符串】-判断一个字符串是否包含某个指定的字符串

作者:互联网

"""check whether some letters lies in one word"""
word = "paramount"
letters = 'para'
if letters in word.lower():
    print("True")
    print(letters)

print("#check if two words share common letters, if so, print them out.")
inter = set(word).intersection(set(letters))
print(inter)

print("#Member Operator in")

str = "Sstring test using member operator in string test using member operator in"
member1 = "str"
member2 = "usi"
print(member1 in str, member2 in str)
print("mem" not in str)

print("#work wtih the methods of string object: find(), rfind(), index(), rindex()")
"""
find()和index()的区别:
方法	区别
 find() 	获取值时,如果要查找的值不存在,会返回-1
 index() 	获取值的索引时,如果不存在值,会报错
 """
print(str.find(member1))
print(str.find(member2))

print(str.rfind(member1))
print(str.rfind(member2))

print(f"index(): {str.index(member1)}")
print(f"index(): {str.index(member2)}")

print(f"rindex(): {str.index(member1)}")
print(f"rindex(): {str.index(member2)}")

str = "I like China"
print(f"count(): {str.count(member1)}")
print(f"count(): {str.count(member2)}")

 

标签:index,letters,Python,print,某个,member1,str,member2,字符串
来源: https://www.cnblogs.com/yidianling/p/16071092.html