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

yipeiwu_com5年前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();

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

相关文章

详解django.contirb.auth-认证

首先看middleware的定义: auth模块有两个middleware:AuthenticationMiddleware和SessionAuthenticationMiddlewar...

基于Python列表解析(列表推导式)

列表解析——用来动态地创建列表 [expr for iter_var in iterable if cond_expr] 例子一: map(lambda x: x**2, ra...

Linux下安装python3.6和第三方库的教程详解

Linux下安装Python3.6和第三方库 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如y...

Python 中Django安装和使用教程详解

Python 中Django安装和使用教程详解

  一、安装     一般使用cmd 安装就可以   手动安装通过下载方式    django官方网站:https://www.djangoproject.com/    python官...

python字符串Intern机制详解

python字符串Intern机制详解

字符串在 Python 中是最简单也是最常用的数据类型之一,在 CPython 中字符串的实现原理使用了一种叫做 Intern(字符串驻留)的技术来提高字符串效率。究竟什么是 inter...