博客
关于我
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 rabbitmq实现简单/持久/广播/组播/topic/rpc消息异步发送可配置Django
查看>>
Python random和json模块
查看>>
Python random模块seed理解
查看>>
python range()函数
查看>>
Python rdflib可传递查询
查看>>
python redis 集群_python 搭建redis集群
查看>>
python redis连接,在Python中使用Redis连接池的正确方法
查看>>
python regex_Python RegEx
查看>>
python requests post 中文结果请求得到unicode
查看>>
Python Requests接口自动化测试实战
查看>>
Python requests模块
查看>>
python request与grequests该如何选择
查看>>
python request模块
查看>>
Python requirements.txt的使用方法
查看>>
Python REST(Web 服务)框架的推荐?
查看>>
Python rsa 加密
查看>>
Python RSA操作
查看>>
Python轻松实现统计学中重要的相关性分析
查看>>
Python scrapy 常见问题及解决 【遇到的坑】
查看>>
Python Seborn热图数据的动态更新
查看>>