Python format number with commas

In this Python tutorial, we will discuss the python format number with commas. Also, we will see these below topics as:

  • Python format number with commas
  • Python add a comma between elements
  • Python format number with commas and round to 2 decimal places

Python format number with commas

Let us see how to format number with commas in python.

In Python, to format a number with commas we will use “{:,}” along with the format() function and it will add a comma to every thousand places starting from left.

Example:

numbers = "{:,}".format(5000000)
print(numbers)

After writing the above code (python format number with commas), Ones you will print “numbers” then the output will appear as a “ 5,000,000”. Here, {:,}.format() will add commas to the number after every thousand places. In this way, we can format the number with commas.

You can refer to the below screenshot for python format number with commas.

Python format number with commas
Python format number with commas

Python add a comma between elements

Let us see how to add a comma between elements in Python.

We will use the “{:,}”.format() function and it will add a comma between the elements after every thousand places starting from left

Example:

def my_value(number):
    return ("{:,}".format(number))
print(my_value(100000000)

After writing the above code (python add a comma between elements), Once you will print “my_value(100000000)” then the output will appear as a “ 100,000,000”. Here, {:,}.format(number) will add commas between elements.

You can refer to the below screenshot for python add a comma between elements.

Python add a comma between elements
Python add a comma between elements

Python format number with commas and round to 2 decimal places

Now, let us see the below example on python format number with commas and round to 2 decimal places.

READ:  Python program to find the area of square

We will use “{:,.2f}”.format()” for float format number with commas as thousands separators. The digits beyond two places after the decimal get ignored.

Example:

my_num = 235478.875425
s = "{:,.2f}".format(my_num)
print(s)

After writing the above code (python format number with commas and round to 2 decimal places), when you will print “s” then the output will appear as “ 235,478.88”. Here, my_num = 235478.875425 is the number in float and we will use {:,.2f}.format(my_num) to add commas to the number after every thousand places and it will round to 2 decimal places.

You can refer to the below screenshot for python format number with commas and round to 2 decimal places.

Python format number with commas and round to 2 decimal places
Python format number with commas and round to 2 decimal places

You may like the following Python tutorials:

In this Python tutorial, we have learned about the python format number with commas. Also, We covered these below topics:

  • Python format number with commas
  • Python add a comma between elements
  • Python format number with commas and round to 2 decimal places