Home > Article > Backend Development > Detailed explanation of finding whether a string exists in python
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!