This article mainly introduces the python to determine whether the string contains a substring of the method, example analysis of Python in and find method to achieve this function, very practical value, the need for friends can refer to the
This example describes how Python determines whether a string contains substrings. Share to everyone for your reference. Specifically as follows:
Python string objects do not have contains methods, do not use the String.contains method to determine whether to include substrings, but Python has a simpler way to replace the contains function.
Method 1: Use the In method to implement contains functionality:
?
1 2 3 |
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
?
1 2 3 4 5 |
s = "This being a string" if S.find ("is") = = -1:print "No ' was ' here!" else:print "Found ' is ' the" string. " |
I hope this article will help you with your Python programming.