示例示例Python 判断字符串包含字符的方法有很多,下面介绍几种常用的方法:使用 in 运算符:
Python 判断字符串包含字符的方法有很多,下面介绍几种常用的方法:
1. 使用 in 运算符:
python
# 定义一个字符串
str = "Hello World"
# 判断字符串中是否包含字符'W'
if 'W' in str:
print("字符串中包含字符'W'")
else:
print("字符串中不包含字符'W'")
2. 使用 find() 方法:
python
# 定义一个字符串
str = "Hello World"
# 判断字符串中是否包含字符'W'
if str.find('W') != -1:
print("字符串中包含字符'W'")
else:
print("字符串中不包含字符'W'")
3. 使用 count() 方法:
python
# 定义一个字符串
str = "Hello World"
# 判断字符串中是否包含字符'W'
if str.count('W') > 0:
print("字符串中包含字符'W'")
else:
print("字符串中不包含字符'W'")
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(33条)