Home  >  Article  >  Backend Development  >  Detailed explanation of finding whether a string exists in python

Detailed explanation of finding whether a string exists in python

高洛峰
高洛峰Original
2017-02-23 15:41:381657browse

The method to find the specified string in python is as follows:

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 splits the string

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();

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more detailed explanations of python’s examples of finding whether a string exists, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn