Python Format Currency with Pound Sign

What is the best way to Python format currency and display a number like 188518982.18 as £188,518,982.18?

I want to format a number in Python to include the pound sign, commas for thousands, and two decimal places. How can I achieve this?

I’ve worked with currency formatting quite a bit, and the easiest way to handle it in Python while respecting locale settings is to use the locale module. Here’s how you can format a number to include the pound sign, thousands separators, and two decimal places automatically:

import locale

locale.setlocale(locale.LC_ALL, '')  # Set locale to the user's default
formatted_currency = locale.currency(188518982.18, grouping=True)
print(formatted_currency)  # Output might vary based on system locale

This method ensures that the currency is formatted correctly according to the active locale. If you want to force it to a specific format like £, you’ll need to explicitly set the locale to en_GB.UTF-8. However, this approach may require system support for that locale.

If you don’t want to rely on locale (since it can be tricky with system dependencies), you can format currency manually using Python’s format() function:

amount = 188518982.18
formatted_currency = "£{:,}".format(amount)
print(formatted_currency)  # Output: £188,518,982.18

This method is simple and works across all systems without worrying about locale settings. However, it doesn’t enforce two decimal places. If you need them, you can adjust the formatting like this:

formatted_currency = "£{:,.2f}".format(amount)
print(formatted_currency)  # Output: £188,518,982.18

It’s a straightforward way to handle Python format currency when you want full control over the output.

Taking it a step further, if you prefer a cleaner and more modern approach, Python’s f-strings make currency formatting even easier:

amount = 188518982.18
formatted_currency = f"£{amount:,.2f}"
print(formatted_currency)  # Output: £188,518,982.18

This is one of the most readable ways to Python format currency, as it keeps everything concise while ensuring thousands separators and two decimal places. No extra modules, no system dependencies—just a clean one-liner that works!

If you need to dynamically change the currency symbol, you can store it in a variable like this:

currency_symbol = "£"
formatted_currency = f"{currency_symbol}{amount:,.2f}"
print(formatted_currency)  # Output: £188,518,982.18

This approach is great when you need flexibility, such as handling multiple currencies dynamically.