Python Glossary Русский

all Python 3localeterm

locale

module

A program's cultural conventions: decimal separator, currency format, and string comparison by a language's alphabet.

Syntax

import locale

The locale is global process state: setlocale changes behaviour for all code and all threads at once, which is why libraries leave it alone. Which locales exist depends on the machine: setlocale(LC_ALL, 'ru_RU.UTF-8') works on one system and raises locale.Error on another. The default file encoding is read with getpreferredencoding or getencoding, not guessed from the locale name.

Examples

import locale
locale.setlocale(locale.LC_ALL, 'C')
print(locale.localeconv()['decimal_point'])  # → .
print(locale.atof('3.5') * 2)  # → 7.0
print(locale.getpreferredencoding())  # → ? зависит от системы

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy