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

yipeiwu_com6年前Python基础

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实现将文本转换成语音的方法

本文实例讲述了python将文本转换成语音的方法。分享给大家供大家参考。具体实现方法如下: # Text To Speech using SAPI (Windows) and Pyt...

利用Python的Twisted框架实现webshell密码扫描器的教程

好久以来都一直想学习windows中得iocp技术,即异步通信,但是经过长时间研究别人的c++版本,发现过于深奥了,有点吃力,不过幸好python中的twisted技术的存在方便了我。...

Python多线程处理实例详解【单进程/多进程】

Python多线程处理实例详解【单进程/多进程】

本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下: python — 多线程处理 1、一个进程执行完后,继续下一个进程 root@72132server:~#...

Python实现的大数据分析操作系统日志功能示例

本文实例讲述了Python实现的大数据分析操作系统日志功能。分享给大家供大家参考,具体如下: 一 代码 1、大文件切分 import os import os.path import...

Python中捕获键盘的方式详解

python中捕获键盘操作一共有两种方法 第一种方法: 使用pygame中event方法 使用方式如下:使用键盘右键为例 if event.type = pygame.KEYDOWN...