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程序设计有所帮助。

相关文章

pytorch:torch.mm()和torch.matmul()的使用

如下所示: torch.mm(mat1, mat2, out=None) → Tensor torch.matmul(mat1, mat2, out=None) → Tensor...

matplotlib实现热成像图colorbar和极坐标图的方法

matplotlib实现热成像图colorbar和极坐标图的方法

热成像图 %matplotlib inline from matplotlib import pyplot as plt import numpy as np def f(x,...

理解python多线程(python多线程简明教程)

对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂。所以,这里力图用简单的例子,让你对多线程有个初步的认识。 单线程   在好些年前的MS-DOS时代,操作系...

Python实现扫描局域网活动ip(扫描在线电脑)

内网的主机都是自动分配ip地址,有时候需要查看下有那些ip在使用,就写了个简单的脚本。 linux和windows下都可以用,用多线程来ping1-255所有的地址,效率不高,2分钟左右...

Python pandas用法最全整理

1、首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as npimport pandas as pd 2、导入CSV或者xlsx文...