Python Glossary Русский

all Python 3Built-in functionsterm

ord

strings

Returns the Unicode code point (an integer) of a character. Takes a string of length 1. The inverse of chr().

Syntax

ord(c)

String comparison and sorting run on exactly these numbers, which is why 'Z' < 'a' (every uppercase Latin letter precedes every lowercase one) and why Cyrillic sorts after all Latin — the source of most "weird" sorted() results. The argument must be exactly one character: ord('ab') and ord('') both raise TypeError, so iterate over the string character by character.

Examples

print(ord('A'))   # → 65
print(ord('a'))   # → 97
print(ord('а'))   # → 1072 (кириллица)
print(ord('0'))   # → 48
print([ord(c) for c in 'ABC'])  # → [65,66,67]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy