Python locale Module — Core Concepts

The locale module is Python’s bridge to the operating system’s localization settings. It provides number formatting, currency display, and locale-aware string comparison — all based on what the OS reports.

Setting a Locale

import locale

# Use the system's default locale
locale.setlocale(locale.LC_ALL, "")

# Or set a specific locale
locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")

The setlocale call configures the C library’s locale, which Python then uses for formatting functions.

Number Formatting

import locale
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")

locale.format_string("%d", 1234567, grouping=True)
# "1,234,567"

locale.format_string("%.2f", 1234.5, grouping=True)
# "1,234.50"

Switch to German:

locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")

locale.format_string("%d", 1234567, grouping=True)
# "1.234.567"

locale.format_string("%.2f", 1234.5, grouping=True)
# "1.234,50"

The atof and atoi functions parse locale-formatted strings back to numbers:

locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
locale.atof("1.234,50")  # 1234.5
locale.atoi("1.234.567")  # 1234567

Currency Formatting

locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
locale.currency(1234.56, grouping=True)
# "$1,234.56"

locale.setlocale(locale.LC_ALL, "ja_JP.UTF-8")
locale.currency(1234, grouping=True)
# "¥1,234"

Locale-Aware Sorting

Different languages sort letters differently. The strxfrm function transforms strings for correct locale-based ordering:

locale.setlocale(locale.LC_ALL, "sv_SE.UTF-8")

words = ["äpple", "zebra", "ängel"]
sorted(words)                          # Python default: ['zebra', 'ängel', 'äpple']
sorted(words, key=locale.strxfrm)      # Swedish order: ['ängel', 'äpple', 'zebra']

In Swedish, ä comes after z. The locale module gets this right.

Querying Locale Information

locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
info = locale.localeconv()

info["decimal_point"]    # ","
info["thousands_sep"]    # "\xa0" (non-breaking space)
info["currency_symbol"]  # "€"
info["mon_decimal_point"]  # ","

Locale Categories

You can set different categories independently:

CategoryControls
LC_ALLEverything
LC_NUMERICNumber formatting
LC_MONETARYCurrency formatting
LC_COLLATEString sorting
LC_TIMEDate/time formatting
LC_CTYPECharacter classification
locale.setlocale(locale.LC_NUMERIC, "en_US.UTF-8")
locale.setlocale(locale.LC_MONETARY, "de_DE.UTF-8")

Available Locales

Locales must be installed on the operating system. Check what’s available:

# Linux/macOS
locale -a | grep -i utf

Common format: language_COUNTRY.encoding — e.g., en_US.UTF-8, fr_FR.UTF-8.

Practical Example: Locale-Aware Output

import locale

locale.setlocale(locale.LC_ALL, "")  # Use system default

amount = 9876543.21
print(locale.format_string("Total: %s", locale.currency(amount, grouping=True)))
# US: "Total: $9,876,543.21"
# Germany: "Total: 9.876.543,21 €"
# Japan: "Total: ¥9,876,543"

This single code path produces correct output regardless of which country the user is in.

Common Misconception

“locale and Babel do the same thing.” The locale module talks to the OS’s C library, so it only works with locales installed on the system. Babel bundles its own CLDR data and works everywhere regardless of OS configuration. Use locale for system-integrated tools; use Babel for portable applications.

The one thing to remember: The locale module gives you OS-native formatting for numbers, currency, and sorting — but it depends on the system having the right locale packages installed.

pythonlocalestandard-libraryformattingsorting

See Also

  • Python Babel Localization Babel teaches your Python app how dates, numbers, and currencies look in every country — not just yours.
  • Python Gettext I18n How Python's gettext module lets your app speak every language without rewriting a single line of logic.
  • Ci Cd Why big apps can ship updates every day without turning your phone into a glitchy mess — CI/CD is the behind-the-scenes quality gate and delivery truck.
  • Containerization Why does software that works on your computer break on everyone else's? Containers fix that — and they're why Netflix can deploy 100 updates a day without the site going down.
  • Python 310 New Features Python 3.10 gave programmers a shape-sorting machine, friendlier error messages, and cleaner ways to say 'this or that' in type hints.