This article mainly introduces python's method to determine whether a string contains a sub-string. the in and find methods in Python are used to implement this function, which is of great practical value, for more information about how to use python to determine whether a string contains a substring, see the following example. Share it with you for your reference. The details are as follows:
The string object in python does not have the contains method. you do not need to use the string. contains method to determine whether a sub-string is contained. However, python has a simpler method to replace the contains function.
Method 1: use the in method to implement the contains function:
Site = 'http: // www.jb51.net/'if "jb51" in site: print ('site contains jb51 ')
Output result: site contains jb51
Method 2: use the find function to implement the contains function
S = "This be a string" if s. find ("is") =-1: print "No 'is' here! "Else: print" Found 'is' in the string ."
I hope this article will help you with Python programming.