Python add commas to number

Python add commas to number

Python allows us to format values to get the final result in our desired look. This article will discuss how we can format numbers in Python and add commas to them.

In this tutorial, all the methods discussed will use the commas as thousand separators.

Using the format() function to add commas to numbers in Python

The format() function allows us to edit and specify the format for a string in Python. We can use it in Python 2.7 and above.

We need to specify the required format to add commas to numbers using this function.

See the code below.

Output:

1,600,000

The {:,} is the required format and the final result is a string. The above code works with float values also. In Python 3.1 and above, the same function can be used in a different way to achieve the same result.

For example,

Output:

1,600,000

Using the fstrings to add commas to numbers in Python

The fstrings provide a quick and efficient way to format values in Python. It is a recent addition to Python, and is available in Python 3.7 and above.

With the format used in the previous method, we can add commas to the numbers.

For example,

Output:

1,600,000

Using the regular expressions to add commas to numbers in Python

Regular expressions allow us to match a pattern on a string in Python. We can use the sub() function with the required pattern to get the numbers in our desired format.

Remember to import the re module to work with regular expressions.

See the following example.

Output:

1,600,000

The sub() function makes the necessary replacements to the value based on the pattern. The above method works only for integers. To work with float values, we have to use %f instead of %d in the function.

For example,

Output:

1,600,000.561,650

Using the locale module to add commas to numbers in Python

The locale module allows us to handle operations based on the location or the preference of the user. Such operations may involve the use of dates, currency, and other values which are regionally dependent. We can use it to add commas to numbers.

We can use the currency() function to display the currency in any desired format. It will add the currency sign and the necessary commas. We will use the en_US region.

For example,

Output:

$1,600,000.00
1,600,000.00

To remove the currency sign, we simply deleted the first character.

We can also use this module with the format() function and fsrings. This way, we do not have to specify the format.

See the code below.

Output:

1,600,000
1,600,000

That’s all about how to add commas to number in Python.

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

  • Python print to file
    17 November

    How to Print to File in Python?

    Table of ContentsThe print() function in pythonHow to Print to file in python?Print to file using the file parameter in the print functionPrint to file by redirecting standard outputConclusion We use the print() function in python to print any text to standard output. In this article, we will see how we can use the print() […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file Conclusion We often use files to store the outputs of our programs in permanent storage. Generally, we work with relative […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.