博客
关于我
Python 判断字符串是否包含子字符串
阅读量:796 次
发布时间:2023-03-07

本文共 431 字,大约阅读时间需要 1 分钟。

  • 第一种方法:in

string = 'helloworld'if 'world' in string:  print 'Exist'else:  print 'Not exist'
View Code
  • 第二种方法:find

1 string = 'helloworld'2 3 if string.find(’world‘) == 5: #5的意思是world字符从那个序开始,因为w位于第六个,及序为5,所以判断54 5   print 'Exist'6 7 else:8 9   print 'Not exist'
View Code
  • 第三种方法:index,此方法与find作用类似,也是找到字符起始的序号

1 if string.index(’world‘) > -1: #因为-1的意思代表没有找到字符,所以判断>-1就代表能找到2 3   print 'Exist'4 5 else:6 7   print 'Not exist'
View Code

 

转载地址:http://ecofk.baihongyu.com/

你可能感兴趣的文章
Python 写Android App性能:入门到高级
查看>>
python 写入json数据到数据库
查看>>
Python 出现 TypeError: ‘encoding‘ is an invalid keyword argument for this function 解决方法
查看>>
python 出现 TypeError: ‘list‘ object is not callable 的解决方法
查看>>
python 函数1
查看>>
python 函数学习
查看>>
Python 函数随笔 map()函数,lambda自定义函数
查看>>
Python 分析天气,告诉你中秋应该去哪里
查看>>
python 列表中dict中key排序
查看>>
python 列表函数
查看>>
Python 列表删除相同的元素
查看>>
python 列表生成式
查看>>
Python 列表解析 大文件
查看>>
python 列表转字典方法
查看>>
Python 列表(List)概述-ChatGPT4o作答
查看>>
Python网络爬虫基础进阶到实战教程
查看>>
Python 初学者需要知道的四条建议
查看>>
Python 判断字符串是否包含子字符串
查看>>
python 判断当前时间是否为零点
查看>>
python 利用pandas读取本地中CSV文件的指定列 列名重命名 并保存回本地
查看>>