Python Glossary Русский

all Python 3unicodedatafunction

unicodedata.bidirectional()

character properties

Returns a character's bidirectional class: 'L' for left-to-right, 'R' and 'AL' for right-to-left, 'EN' for digits and so on.

Syntax

unicodedata.bidirectional(chr, /)

Hebrew is 'R', while Arabic letters have a separate class 'AL', so a right-to-left check must look for both. Digits and spaces are not 'L' but 'EN' and 'WS': the string '123' has no direction of its own. For unassigned characters the answer depends on the database version and hence on the Python version.

Examples

import unicodedata
print([unicodedata.bidirectional(c) for c in 'aא\u06275 '])  # → ['L', 'R', 'AL', 'EN', 'WS']
def is_rtl(text):
    return any(unicodedata.bidirectional(c) in ('R', 'AL') for c in text)
print(is_rtl('abc'), is_rtl('שלום'))  # → False True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy