Menu Close

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

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.

Posted in Python, Python Answers