Analyzing the relationship string containing python

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

1. members operator in

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

2. string module find () / rfind () method

>>> 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. Use the string module index () / rindex () method 
index () / rindex () method like find () / rfind () method, but can not find time substring will be reported a ValueError exception.

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. String object find () / rfind (), index () / rindex () and the count () method

>>> 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

Guess you like

Origin www.cnblogs.com/xiohao/p/11241568.html