【Python 字串】-判斷一個字串是否包含某個指定的字串
阿新 • • 發佈:2022-03-29
"""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)}")