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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.