Python 中检查字符串是否不包含指定的子字符串

使用 not in 运算符检查字符串是否不包含给定的子字符串,例如 如果子字符串不在字符串中:。

如果子字符串不包含在字符串中,则 not in 运算符将返回 True,否则返回 False。

# ✅ 检查字符串是否不包含给定的子字符串
string = 'zadmei.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', 'zadmei', '.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 = 'zadmei.com'

print('yik' in string)  # 👉️ True
print('yik' not in string)  # 👉️ False

print('abc' in string)  # 👉️ False
print('abc' not in string)  # 👉️ True

如果我们需要以不区分大小写的方式检查字符串是否不包含子字符串,请将两个字符串都转换为小写。

string = 'zadmei.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 列表中的任何字符串

检查字符串是否不包含列表中的任何字符串:

  1. 使用生成器表达式迭代列表。
  2. 检查每个列表项是否不包含在字符串中。
  3. 如果所有列表项都满足条件,则字符串不包含列表中的任何字符串。
my_list = ['fql', 'zadmei', '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', 'zadmei', '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', 'zadmei', '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', 'zadmei', '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', 'zadmei', 'com']

my_str = 'abc fql 2468'


matches = [item for item in my_list if item in my_str]
print(matches)  # 👉️ ['fql']

列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。

新列表仅包含其他字符串中包含的字符串。