Python “in” Operator

The Python “in” operator checks if an element is present in a sequence or if a substring exists in a string. The “in” b returns True if the element or substring is found; otherwise, it returns False. The “in” operator can be used with various data structures, such as lists, strings, tuples, sets, and dictionaries (when checking for keys).

Example 1

strA = "Game of Thrones was the best show on the planet"

print("Thrones" in strA)


Using the in operator

As you can see, an “in operator” returns the True value when the substring exists in the string. Otherwise, it returns false. The above approach is very straightforward, clean, readable, and conversational.

Example 2

Let’s see the in operator in the if..else condition.

strA = "Game of Thrones was the best show on the planet"

if "Thrones" in strA: 
  print('It exists')
  print('Does not exist')


in operator in if..else


That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.