【Python 字符串】-判断一个字符串是否包含某个指定的字符串
2022/3/29 12:56:23
本文主要是介绍【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)}")
这篇关于【Python 字符串】-判断一个字符串是否包含某个指定的字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南