[Python] Check whether a string contains a substring

Created on November 30, 2026

Use the in operator to check whether one string contains another. This example prints a different message depending on whether abc123def contains 123.

if '123' in 'abc123def':
      print('Substring found')
else:
      print('Substring not found')

Output

Substring found
Python