Python Glossary Русский

all Python 3Strings (str)term

in / not in for strings

search

Checks whether one string is a substring of another. Returns a bool.

Syntax

'sub' in s  |  'sub' not in s

On strings the test is substring-based, not element-based: 'py' in 'python' is True, while 'py' in ['python'] is False because a list compares whole items. The empty string is contained in every string, even an empty one, and the check is case-sensitive — lowercase both sides when case should not matter.

Examples

print('py' in 'python')
# → True
print('xyz' in 'python')
# → False
print('xyz' not in 'python')
# → True
print('' in 'any')
# → True (пустая всегда входит)
print('P' in 'python')
# → False (регистрозависимо)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy