Does Python provide a method for checking if a string contains a substring?

Does Python provide a method for checking if a string contains a substring?

Hi Archna,

To check if a string contains a substring in Python, you can use the in operator:

if "substring" in my_string:
    # do something

You can also use the str.find() method, which returns the lowest index of the substring if it is found in the string, or -1 if it is not found:

if my_string.find(“substring”) != -1: # do something

Using regular expressions (re module) to search for the substring:

import re

if re.search(“substring”, my_string): # do something