How to check if a string contains a specific substring in Python?

To check if a string contains a specific substring in Python, you can use the in operator or the find() method. Here's an example of how to do it:

  1. Using the in operator:
string = "This is a sample string" substring = "sample" if substring in string: print("Substring found!") else: print("Substring not found.")
  1. Using the find() method:
string = "This is a sample string" substring = "sample" if string.find(substring) != -1: print("Substring found!") else: print("Substring not found.")

Both methods will return True if the substring is found in the string, and False otherwise.