str contains in python

To check if a string contains a specific substring in Python, you can use the in keyword.

Here is an example:

main.py
string = "Hello, World!"
substring = "World"

if substring in string:
    print("Substring found!")
else:
    print("Substring not found!")
140 chars
8 lines

This code will output "Substring found!" since the substring "World" is present in the string "Hello, World!".

You can also use the str.__contains__() method to achieve the same result:

main.py
string = "Hello, World!"
substring = "World"

if string.__contains__(substring):
    print("Substring found!")
else:
    print("Substring not found!")
151 chars
8 lines

Both methods will give you the same output.

related categories

gistlibby LogSnag