one string contains part of the other in python

You can check if one string is a substring of another string in Python using the in keyword. Here's an example:

main.py
string1 = "hello world"
string2 = "world"

if string2 in string1:
    print("string2 is a substring of string1")
else:
    print("string2 is not a substring of string1")
170 chars
8 lines

Output:

main.py
string2 is a substring of string1
34 chars
2 lines

In this example, we check if string2 is a substring of string1 using the in keyword. Since string2 is indeed a part of string1, the output is "string2 is a substring of string1".

related categories

gistlibby LogSnag