python七种方法判断字符串是否包含子串

作者:写代码的明哥 时间:2023-09-19 04:06:20 

1. 使用 in 和 not in

in 和 not in 在 Python 中是很常用的关键字,我们将它们归类为 成员运算符。

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


>>> "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. 通过魔法方法

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

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

示例如下:


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

这个用法与使用 in 和 not 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://www.tuicool.com/articles/M3IJ3uR

标签:python,字符串,子串
0
投稿

猜你喜欢

  • python实现蒙特卡罗方法教程

    2023-01-29 16:36:02
  • Python 实现文件的全备份和差异备份详解

    2023-07-16 20:20:36
  • Zend Studio去除编辑器的语法警告设置方法

    2023-10-11 17:10:15
  • 停止密码输入掩饰?[译]

    2009-07-22 18:44:00
  • 网站LOGO设计规范的思考--2.网络LOGO的设计

    2007-10-14 11:02:00
  • Python操作json的方法实例分析

    2022-10-13 10:39:09
  • on error goto (Vbscript)和try catch

    2008-08-04 13:22:00
  • spyder常用快捷键(分享)

    2021-04-01 16:18:17
  • 使用Python创建websocket服务端并给出不同客户端的请求

    2023-08-01 23:29:32
  • Golang二维切片初始化的实现

    2024-05-09 14:57:54
  • IDEA使用JDBC安装配置jar包连接MySQL数据库

    2024-01-23 17:43:21
  • 用javascript结合Cookies记录浏览历史

    2008-02-12 12:37:00
  • python正则表达式中匹配次数与贪心问题详解(+ ?*)

    2021-04-24 20:52:04
  • python修改字典键(key)的方法

    2023-04-16 20:26:57
  • 一文带你解密Python可迭代对象的排序问题

    2023-11-26 00:53:13
  • python实现猜数游戏(保存游戏记录)

    2022-08-31 09:39:54
  • python实现微信小程序用户登录、模板推送

    2021-10-15 23:25:17
  • python获取文件真实链接的方法,针对于302返回码

    2023-11-23 04:31:03
  • mysql query browser中文乱码的解决方法

    2024-01-17 14:44:51
  • python画图常规设置方式

    2023-11-25 03:52:04
  • asp之家 网络编程 m.aspxhome.com