Categories
Python Answers

How to print number with commas as thousands separators with Python?

Spread the love

Sometimes, we want to print number with commas as thousands separators with Python.

In this article, we’ll look at how to print number with commas as thousands separators with Python.

How to print number with commas as thousands separators with Python?

To print number with commas as thousands separators with Python, we can use a format string with the locale library.

For instance, we write

import locale
locale.setlocale(locale.LC_ALL, '') 

print(f'{value:n}')

to set the locale with

locale.setlocale(locale.LC_ALL, '') 

Then we format the value number to a number string with thousands separators with

f'{value:n}'

Conclusion

To print number with commas as thousands separators with Python, we can use a format string with the locale library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *