Python Glossary Русский

all Python 3until 3.12Removed from the standard libraryfunction

locale.format()

removed

Formatted a single number using the current locale's rules. Removed in Python 3.12; use locale.format_string().

Syntax

locale.format(format, val, grouping=False, monetary=False)  # до 3.12

locale.format('%.2f', x, grouping=True) put one value into a template with a single % specifier and applied the locale's separators. It silently accepted extra text around the specifier and behaved unpredictably, while format_string() does the same reliably for any template. format() was deprecated in Python 3.7 and removed in 3.12. How to rewrite: locale.format(fmt, value, grouping) becomes locale.format_string(fmt, value, grouping) with the same arguments. For money use locale.currency(); for a plain number without a template use locale.str().

Examples

# Python 3.12+
import locale
try:
    locale.format('%d', 5)
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.12+
import locale
print(locale.format_string('%.2f', 3.14159))  # → 3.14
print(locale.format_string('%d items, %s', (3, 'ok')))  # → 3 items, ok
import locale
print(locale.format('%.2f', 3.14159))  # → 3.14

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy