In Python, there is no dedicated string.contains() or string.indexof() method for checking if a string contains a substring. However, there are alternative approaches to achieve this functionality.
The 'in' operator can be used to test if a substring is present within a string. It compares the substring to the entire string and returns True if it is found.
if "blah" in some_string: # Code to be executed if "blah" is in some_string else: # Code to be executed if "blah" is not in some_string
Note: The 'in' operator is case-sensitive.
string.find() method: Returns the index of the first occurrence of the substring, or -1 if not found.
index = some_string.find("blah")
string.startswith() method: Checks if the string starts with the specified substring.
if some_string.startswith("blah"): # Code to be executed if some_string starts with "blah"
string.endswith() method: Checks if the string ends with the specified substring.
if some_string.endswith("blah"): # Code to be executed if some_string ends with "blah"
The above is the detailed content of How Can I Check if a Python String Contains a Substring?. For more information, please follow other related articles on the PHP Chinese website!