CBSE Class 11 : Python Strings 7

String Membership Test

We can test if a sub string exists within a string or not, using the keyword in and not in.

In operator

n=”computer”
print(“o” in n)
print(“t” in n)
print(“e” in n)
print(“w” in n)
print(“v” in n)
print(“z” in n)
True
True
True
False
False
False

n=”Hello World”
z=”o” in n
print(“z = “,z)
z=”w” in n
print(“z = “,z)
z=”d” in n
print(“z = “,z)
z = True
z = False
z = True

not In operator

n=”computer”
print(“o” not in n)
print(“t” not in n)
print(“e” not in n)
print(“w” not in n)
print(“v” not in n)
print(“z” not in n)
False
False
False
True
True
True