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:
in
operator:string = "This is a sample string"
substring = "sample"
if substring in string:
print("Substring found!")
else:
print("Substring not found.")
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.