1. Use the member operator in

>>> s='nihao,shijie'
>>> t='nihao'
>>> result = t in s
>>> print result
True
12345
Copy the code

2. Use the find()/rfind() methods of the String module

>>> 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 123456789Copy the code

Index ()/rindex() is the same as find()/rfind(), except that ValueError is raised when a substring is not found.

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
12345678910111213
Copy the code

4. Use the find()/rfind(), index()/rindex(), and count() methods for string objects (this method determines how many identical objects or strings are included in the total collection)

>>> 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
1234567891011
Copy the code

Well, that’s all for today’s sharing. If you’re interested in Python, join us.Python Learning communicationSkirt 】, receive free learning materials and source code.