Add Commas to Number in Python

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 fstrings. 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

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b2e742aa57e145093391/] [crayon-63b2e742aa584914141606/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b2e742aa758977709593/] [crayon-63b2e742aa75b617219646/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63b2e742aa885559579719/] [crayon-63b2e742aa888294554165/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

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.