Python 中检查字符串是否不包含指定的子字符串
使用 not in 运算符检查字符串是否不包含给定的子字符串,例如 如果子字符串不在字符串中:。
如果子字符串不包含在字符串中,则 not in 运算符将返回 True,否则返回 False。
# ✅ 检查字符串是否不包含给定的子字符串
string = 'jiyik.com'
substring = 'abc'
if substring not in string:
# 👇️ this runs
print('The string does NOT contain the substring')
else:
print('The string contains the substring')
# ---------------------------------------------------
# ✅ 检查字符串是否不包含列表中的任何字符串
a_list = ['fql', 'jiyik', '.com']
string = '123 fql abc 456'
not_contains_any = all(item not in string for item in a_list)
print(not_contains_any) # 👉️ False
in
运算符测试成员资格。 例如,如果 x 是 s 的成员,则x in s
的计算结果为 True,否则计算结果为 False。
x not in s
返回 x in s
的否定。
string = 'jiyik.com'
print('yik' in string) # 👉️ True
print('yik' not in string) # 👉️ False
print('abc' in string) # 👉️ False
print('abc' not in string) # 👉️ True
如果我们需要以不区分大小写的方式检查字符串是否不包含子字符串,请将两个字符串都转换为小写。
string = 'jiyik.com'
substring = 'BOB'
if substring.lower() not in string.lower():
print('The string does NOT contain the substring')
else:
# 👇️ this runs
print('The string contains the substring')
str.lower
方法返回字符串的副本,其中所有大小写字符都转换为小写。
通过将两个字符串转换为相同的大小写,我们可以执行不区分大小写的成员资格测试。
检查字符串是否不包含 Python 列表中的任何字符串
检查字符串是否不包含列表中的任何字符串:
- 使用生成器表达式迭代列表。
- 检查每个列表项是否不包含在字符串中。
- 如果所有列表项都满足条件,则字符串不包含列表中的任何字符串。
my_list = ['fql', 'jiyik', 'com']
my_str = 'abc fql 2468'
# ✅ 检查字符串是否不包含列表中的任何字符串
not_contains = all(item not in my_str for item in my_list)
print(not_contains) # 👉️ False
if not_contains:
print('The string does NOT contain any string from the list')
else:
# 👇️ this runs
print('The string contains at least one of the strings from the list')
# ---------------------------------------------------------------
# ✅ 检查字符串是否至少包含列表中的一个字符串
contains = any(item in my_str for item in my_list)
print(contains) # 👉️ True
# ---------------------------------------------------------------
# ✅ 查找包含在字符串中的列表项
matches = [item for item in my_list if item in my_str]
print(matches) # 👉️ ['fql']
我们使用生成器表达式来遍历列表。
生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。
my_list = ['fql', 'jiyik', 'com']
my_str = 'abc fql 2468'
not_contains = all(item not in my_str for item in my_list)
print(not_contains) # 👉️ False
在每次迭代中,我们检查当前列表项是否不包含在字符串中并返回结果。
in 运算符测试成员资格。 例如,如果 x 是 s 的成员,则 x in s
的计算结果为 True,否则计算结果为 False。
x not in s
返回 x in s
的否定。
all()
内置函数将可迭代对象作为参数,如果可迭代对象中的所有元素都为真(或可迭代对象为空),则返回 True。
如果我们需要检查字符串是否至少包含列表中的一个字符串,请改用 any()
函数。
my_list = ['fql', 'jiyik', 'com']
my_str = 'abc fql 2468'
contains = any(item in my_str for item in my_list)
print(contains) # 👉️ True
if contains:
print('The string contains at least one of the strings from the list')
else:
print('The string does NOT contain any of the strings from the list')
any
函数将一个可迭代对象作为参数,如果可迭代对象中的任何元素为真,则返回 True。
在每次迭代中,我们检查当前列表项是否包含在字符串中并返回结果。
如果任何列表项满足条件,则
any()
函数短路并返回 True。
如果我们需要执行不区分大小写的成员资格测试,请将两个字符串都转换为小写。
my_list = ['fql', 'jiyik', 'com']
my_str = 'ABC FQL 2468'
contains = any(item.lower() in my_str.lower() for item in my_list)
print(contains) # 👉️ True
if contains:
print('The string contains at least one of the strings from the list')
else:
print('The string does NOT contain any of the strings from the list')
str.lower
方法返回字符串的副本,其中所有大小写字符都转换为小写。
将两个字符串都转换为小写或大写允许我们以不区分大小写的方式测试成员资格。
如果我们需要查找字符串中包含的列表项,请使用列表推导。
my_list = ['fql', 'jiyik', 'com']
my_str = 'abc fql 2468'
matches = [item for item in my_list if item in my_str]
print(matches) # 👉️ ['fql']
列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。
新列表仅包含其他字符串中包含的字符串。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。