python 查找字符串是否存在实例详解

作者:lqh 时间:2023-09-22 21:31:17 

python中查找指定的字符串的方法如下:

code


#查询
def selStr():
 sStr1 = 'jsjtt.com'
 sStr2 = 'com'
 #index查询某个字符串,返回索引
 nPos = sStr1.index(sStr2)
 if(nPos >=0):
   print 'sStr1中包括sStr2中的字符'
 print nPos

#find 方法如果没有查询到返回-1
 nPos2 = sStr1.find('abc')
 print nPos2
 #查询到返回字符所在位置
 print sStr1.find('com')

selStr();

 python分割字符串


def split():
 sStr1 = 'ab,cde,fgh,ijk'
 sStr2 = ','
 #用find查找逗号所在的索引位置
 sStr1 = sStr1[sStr1.find(sStr2) + 1:]
 print sStr1
 #使用split函数分割字符串
 s = 'ab,cde,fgh,ijk'
 result = s.split(',')
 print(result)
 print(result[0])
split();

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:python,查找,字符串
0
投稿

猜你喜欢

  • zookeeper python接口实例详解

    2023-03-11 01:34:48
  • Python实现压缩文件夹与解压缩zip文件的方法

    2023-07-31 20:43:09
  • Python 经典面试题 21 道【不可错过】

    2023-10-29 04:56:20
  • Python 如何解决稀疏矩阵运算

    2022-02-13 21:59:01
  • 基于python二叉树的构造和打印例子

    2023-05-16 15:35:45
  • Python利用pywin32实现自动操作电脑

    2024-01-03 03:35:59
  • MATLAB中print函数使用示例详解

    2023-11-18 04:18:04
  • python数据结构算法分析

    2022-06-11 02:57:15
  • WEB移动应用框架构想

    2010-09-28 16:26:00
  • VS Code配置Go语言开发环境的详细教程

    2024-05-11 09:08:31
  • 一个导航的前端实现

    2008-11-13 12:41:00
  • 交互因视觉设计而更完美

    2008-05-31 17:22:00
  • pytorch神经网络之卷积层与全连接层参数的设置方法

    2023-07-11 14:21:27
  • MySQL查询条件常见用法详解

    2024-01-16 10:10:22
  • Python+OpenCV采集本地摄像头的视频

    2021-12-06 23:11:31
  • Python shapefile转GeoJson的2种方式实例

    2023-02-20 01:07:52
  • TensorFlow的权值更新方法

    2022-12-24 21:41:08
  • mysql 索引使用及优化详情

    2024-01-24 16:06:56
  • python微信跳一跳游戏辅助代码解析

    2021-06-10 21:42:29
  • js实现点击后将文字或图片复制到剪贴板的方法

    2024-04-30 09:51:07
  • asp之家 网络编程 m.aspxhome.com