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

yipeiwu_com6年前Python基础

本文实例讲述了python判断字符串是否包含子字符串的方法。分享给大家供大家参考。具体如下:

python的string对象没有contains方法,不用使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数。

方法1:使用 in 方法实现contains的功能:

site = '//www.jb51.net/'
if "jb51" in site:
   print('site contains jb51')

输出结果:site contains jb51

方法2:使用find函数实现contains的功能

s = "This be a string"
if s.find("is") == -1:
  print "No 'is' here!"
else:
  print "Found 'is' in the string."

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python如何应用cx_Oracle获取oracle中的clob字段问题

最近在用Python编写连接数据库获取记录的脚本,其中用到了cx_Oracle模块。它的语法主要如下: cx_Oracle.connect('username','pwd','IP/...

ubuntu安装sublime3并配置python3环境的方法

最近有一些烦,虚拟机跑代码,跑着跑着存储不够,我就去扩大磁盘,结果虚拟机崩了,试了一上午的修复办法,仍然无法修复,于是只能重装虚拟机,配置各种环境,这里总结一下Ubuntu中配置subl...

python使用urllib2模块获取gravatar头像实例

Gravatar注册地址: https://en.gravatar.com/ 复制代码 代码如下:"""`Gravatar <https://en.gravatar.com/sit...

PyQt5 pyqt多线程操作入门

首先来看一个例子: # coding=utf-8 __author__ = 'a359680405' from PyQt5.QtCore import * from PyQ...

Python 找到列表中满足某些条件的元素方法

如下所示: a = [0, 1, 2, 3, 4, 0, 2, 3, 6, 7, 5] selected = [x for x in a if x in range(1, 5)] #...