Python Glossary Русский

all Python 3Strings (str)term

String comparison

comparison

Strings are compared lexicographically by the Unicode code points of their characters.

Syntax

s1 == s2  |  s1 < s2  |  s1 > s2

Code point order is not alphabetical order: every uppercase ASCII letter sorts before every lowercase one, so 'Zoo' < 'apple' is True, and Cyrillic or accented letters form their own separate block. For human-friendly sorting pass a key such as key=str.lower or key=str.casefold. The other common slip is comparing numbers as text: '10' < '9' is True because only the first characters decide.

Examples

print('abc' == 'abc')
# → True
print('abc' < 'abd')
# → True ('c' < 'd')
print('b' > 'a')
# → True
print('Z' < 'a')
# → True (ASCII: Z=90, a=97)
print('abc' == 'ABC')
# → False (регистрозависимо)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy