Python判断字符串是否包含特定子字符串的多种方法(7种方法)

作者:赵孝正 时间:2021-09-20 02:22:51 

我们经常会遇这样一个需求:判断字符串中是否包含某个关键词,也就是特定的子字符串。比如从一堆书籍名称中找出含有“python”的书名。

判断两个字符串相等很简单,直接 == 就可以了。其实判断包含子串也非常容易,而且还不止一种方法。下面我们就给大家分享 7 种可以达到此效果的方法:

1、使用 in 和 not in

innot inPython 中是很常用的关键字,我们将它们归类为成员运算符。

使用这两个成员运算符,可以很让我们很直观清晰的判断一个对象是否在另一个对象中,示例如下:

>>> "llo" in "hello, python"
True
>>>
>>> "lol" in "hello, python"
False

2、使用 find 方法

使用 字符串 对象的 find 方法,如果有找到子串,就可以返回指定子串在字符串中的出现位置,如果没有找到,就返回 -1

>>> "hello, python".find("llo") != -1
True
>>> "hello, python".find("lol") != -1
False
>>

3、使用 index 方法

字符串对象有一个 index 方法,可以返回指定子串在该字符串中第一次出现的索引,如果没有找到会抛出异常,因此使用时需要注意捕获。

def is_in(full_str, sub_str):
   try:
       full_str.index(sub_str)
       return True
   except ValueError:
       return False

print(is_in("hello, python", "llo"))  # True
print(is_in("hello, python", "lol"))  # False

4、使用 count 方法

利用和 index 这种曲线救国的思路,同样我们可以使用 count 的方法来判断。

只要判断结果大于 0 就说明子串存在于字符串中。

def is_in(full_str, sub_str):
   return full_str.count(sub_str) > 0

print(is_in("hello, python", "llo"))  # True
print(is_in("hello, python", "lol"))  # False

5、通过魔法方法

在第一种方法中,我们使用 innot in 判断一个子串是否存在于另一个字符中,实际上当你使用 innot in 时,Python 解释器会先去检查该对象是否有__contains__魔法方法。

若有就执行它,若没有,Python 就自动会迭代整个序列,只要找到了需要的一项就返回 True

示例如下;

>>> "hello, python".__contains__("llo")
True
>>>
>>> "hello, python".__contains__("lol")
False
>>>

这个用法与使用 innot in 没有区别,但不排除有人会特意写成这样来增加代码的理解难度。

6、借助 operator

operator模块是 python 中内置的操作符函数接口,它定义了一些算术和比较内置操作的函数。operator模块是用 c 实现的,所以执行速度比 python 代码快。

operator 中有一个方法 contains 可以很方便地判断子串是否在字符串中。

>>> import operator
>>>
>>> operator.contains("hello, python", "llo")
True
>>> operator.contains("hello, python", "lol")
False
>>>

7、使用正则匹配

说到查找功能,那正则绝对可以说是专业的工具,多复杂的查找规则,都能满足你。

对于判断字符串是否存在于另一个字符串中的这个需求,使用正则简直就是大材小用。

import re

def is_in(full_str, sub_str):
   if re.findall(sub_str, full_str):
       return True
   else:
       return False

print(is_in("hello, python", "llo"))  # True
print(is_in("hello, python", "lol"))  # False

来源:https://blog.csdn.net/weixin_46713695/article/details/129441565

标签:Python,判断字符串,特定子串
0
投稿

猜你喜欢

  • Python利用fastapi实现上传文件

    2023-12-09 05:54:40
  • PyCharm在新窗口打开项目的方法

    2023-06-14 23:00:29
  • PHP取余函数介绍MOD(x,y)与x%y

    2023-10-02 20:15:55
  • 一文速学Python+Pyecharts绘制树形图

    2023-07-28 12:05:27
  • 使用Python生成url短链接的方法

    2021-05-05 02:55:12
  • IE的有条件注释详解(附实例代码)

    2009-03-31 13:01:00
  • django_orm查询性能优化方法

    2021-03-01 20:30:19
  • sql server海量数据库的查询优化及分页算法方案

    2010-07-02 21:17:00
  • python re.match()用法相关示例

    2023-07-21 05:12:30
  • Python无损音乐搜索引擎实现代码

    2021-01-28 19:19:02
  • pytorch使用 to 进行类型转换方式

    2022-10-05 23:28:27
  • Laravel框架实现点播上传阿里云功能

    2023-06-13 20:13:30
  • Python解析m3u8拼接下载mp4视频文件的示例代码

    2022-04-22 13:20:16
  • python opencv实现信用卡的数字识别

    2023-07-05 02:20:23
  • SQL Transcation的一些总结分享

    2012-08-21 10:21:28
  • matplotlib基础绘图命令之imshow的使用

    2023-12-27 17:08:58
  • SQL Server性能的改进得益于逻辑数据库设计

    2009-10-23 13:55:00
  • 使用jupyter notebook运行python和R的步骤

    2023-03-30 18:22:50
  • asp如何创建一个Exchange用户?

    2009-11-14 20:52:00
  • 浅谈Tensorflow 动态双向RNN的输出问题

    2022-10-16 21:30:35
  • asp之家 网络编程 m.aspxhome.com