Sling Academy
Home/Python/Python: How to format large numbers with comma separators

Python: How to format large numbers with comma separators

Last updated: June 04, 2023

In Python, there are two easy ways to format large numbers with commas as separators to improve the readability of information (when printing to the console or displaying on the web).

Using f-strings (Python 3.6+)

Just enclose your number in a pair of curly braces {} within an f-strings, then use a comma as the format specifier.

Example:

number = 12345678901234567

# Format the number with comma separators
formatted_number = f'{number:,}'  
print(formatted_number)

Output:

12,345,678,901,234,567

Note that formatted_number is a string.

Using the format() function

Pass your number as the first argument and pass a comma as the second argument to the format() function, and you will get the desired result.

Example:

number = 10000000000000000

 # Format the number with comma separators
formatted_number = format(number, ',') 
print(formatted_number)

Output:

10,000,000,000,000,000

That’s it. Happy coding!

Next Article: Python: How to Convert a Number to a Byte Array

Previous Article: Python: Reverse the Order of Digits in a Number (2 Approaches)

Series: Python – Numbers & Math Tutorials

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots