脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 查找字符串是否存在实例详解

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

2020-09-18 10:46脚本之家 Python

这篇文章主要介绍了python 查找字符串是否存在实例详解的相关资料,需要的朋友可以参考下

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

code

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#查询
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分割字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
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();

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

延伸 · 阅读

精彩推荐