转自---http://blog.csdn.net/yl2isoft/article/details/52079960

1.使用成员操作符 in

>>> s='nihao,shijie'
>>> t='nihao'
>>> result = t in s
>>> print result
True

2.使用string模块的find()/rfind()方法

>>> import string
>>> s='nihao,shijie'
>>> t='nihao'
>>> result = string.find(s,t)!=-1
>>> print result
True
>>> result = string.rfind(s,t)!=-1
>>> print result
True

3.使用string模块的index()/rindex()方法 
index()/rindex()方法跟find()/rfind()方法一样,只不过找不到子字符串的时候会报一个ValueError异常。

import string

def find_string(s,t):
try:
string.index(s,t)
return True
except(ValueError):
return False s='nihao,shijie'
t='nihao'
result = find_string(s,t)
print result #True

4.使用字符串对象的find()/rfind()、index()/rindex()和count()方法

>>> s='nihao,shijie'
>>> t='nihao'
>>> result = s.find(t)>=0
>>> print result
True
>>> result=s.count(t)>0
>>> print result
True
>>> result=s.index(t)>=0
>>> print result
True 参考链接:https://www.cnblogs.com/johnson-yuan/p/7910087.html

python判断字符串包含关系的更多相关文章

  1. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  2. 用Shell判断字符串包含关系的方法小结

     这篇文章主要给大家介绍了关于用Shell判断字符串包含关系的几种方法,其中包括利用grep查找.利用字符串运算符.利用通配符.利用case in 语句以及利用替换等方法,每个方法都给出了详细的示例代 ...

  3. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  4. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  5. Python判断字符串编码以及编码的转换

    转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...

  6. python判断字符串中是否包含子字符串

    python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1:     print('存在') else:     print('不存在' ...

  7. 转 Shell判断字符串包含关系的几种方法

    https://blog.csdn.net/rznice/article/details/71086839 Shell中判断字符串包含关系的方法: 1.通过grep来判断:12str1="a ...

  8. python判断字符串是否是json格式方法分享

    python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例:   ...

  9. Python判断字符串是否为字母或者数字

    严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...

随机推荐

  1. FreeRTOS任务运行时间信息统计

    相关宏的设置 configGENERATE_RUN_TIME_STATS //使能 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() //配置一个高精度定时器/计数器提 ...

  2. flashdevelop调用ios方法

    来源:http://blog.csdn.net/zu12jing/article/details/7331397 flash开发工具用的是flashdevelop(由于flashdevelop还能直接 ...

  3. ConcurrentHashMap源码解析(JDK8)

    首先看看CHM的重要成员变量: public class ConcurrentHashMap<K,V> extends AbstractMap<K,V> implements ...

  4. Java中数组的定义,初始化和使用

    定义:数组是数据类型相同的,用一个标志符名称封装在一起的一个对象序列或基本类型数据序列(一组相同数据类型元素的集合,并且分配一块连续的内存来存储). 格式:int[] a1(常用)  或者 int a ...

  5. python Linux 环境 (版本隔离工具)

    python Linux 环境 (版本隔离工具) 首先新建用户,养成良好习惯useradd python 1.安装pyenv GitHub官网:https://github.com/pyenv/pye ...

  6. MySQL Hardware--FIO压测

    FIO参数 .txt 支持文件系统或者裸设备,-filename=/dev/sda2或-filename=/dev/sdb direct= 测试过程绕过机器自带的buffer,使测试结果更真实 rw= ...

  7. 2019.7月-前端面试总结(H5+C3+JS+ES6+Vue+浏览器)

    第二次面试 HTML HTML5中的新标签,举例一下 canvas绘画,本地离线存储localStorage,sessionStorage,video和audio元素,语义化元素,表单类型(date, ...

  8. gitlab自带的Nginx与原Nginx冲突的解决方案

    gitlab 推荐方案2 默认情况下,gitlab使用自带的Nginx,占用80端口,这样就与系统原本安装的Nginx冲突.导致其中一个nginx无法启动 我的gitlab可以正常启动,当再部署一个接 ...

  9. table标签修改tr,td标签的行距

    修改tr标签的行距,tbale标签的td标签间距 看如下Css文件的代码,其都没有table的tr行距产生效果 tr{ margin-top: 10px; padding: 10px; } td{ m ...

  10. python的isinstance()函数

    以下是使用isinstance()函数的实例: a = isinstance(a,int) # 结果返回 True isinstance(a,str) # 结果返回 False 即:第1个参数是第2个 ...