Python Number Formatting


Introduction to Number Formatting

Python has built-in support for converting numbers to human-readable formats. Whether it is currency, decimals, the width of the number, or alignment, python has everything covered. For instance, we might wish to add a percentage sign after the number or include commas in the currency.

Python f-string

Formatting a number with f-string is easy and powerful. It makes it easy to mix numbers and other data types. We simply use the upper or lowercase f before the quotation mark (quotes). Any variable we want to insert into the string is enclosed in the curly brackets.

We can do direct calculations inside the curly brackets.

The string part to be printed exactly as typed in the f-string is called the literal part, and the one in curly brackets is called the expression part.

f string in python

Simply printing a number in srting.

In [1]:
item_price = 25
print(f"Your bill : {item_price}")
Your bill : 25

Calculation in f-string.

In [2]:
item_price = 25
quantity = 2
print(f"Your bill : {item_price * quantity}")
Your bill : 50

Working with currency

When working with currencies such as the dollar, we may need to include a comma and only show the value to two decimal places.

To achieve this, we have to use a colon (:) immediately after the variable and then a comma(,). If we want to limit a number to two decimal places, we can write “.2f” following a comma. “.2f ” signifies “two decimal places, fixed”.

Example:

print(f"Your bill : ${price:,.2f}")

Simply printing price.

In [1]:
price = 75365.7
(f"Your bill : ${price}")
Out[1]:
'Your bill : $75365.7'

printing comma in price.

In [2]:
price = 75365.7
print(f"Your bill : ${price:,}")
Your bill : $75,365.7

2 decimal places only.

In [3]:
price = 75365.7
print(f"Your bill : ${price:,.2f}")
Your bill : $75,365.70

Working with percent numbers

To represent a percentage number, for example, a discount of 7.5%, will be written as 0.075 in the code.

discount = 0.075

To show a percentage sign in a print statement, we must use a colon(:) after the variable name, followed by the “.2%”. It will show the number up to two decimal places and add a percentage(%) sign.

Example:

print(f"Discount : {discount:.2%}")

No formatting.

In [1]:
discount = 0.075
print(f"Discount : {discount}")
Discount : 0.075

Formatting on discount.

In [2]:
discount = 0.075
print(f"Discount : {discount:.2%}")
Discount : 7.50%

Formatting width

When we want to fix the width of the numbers, then in the f-string, after the colon, we can use the number of characters we wish to use.

Example:

print(f"Item 1 price : ${item1_price:15} only")

Without width.

In [1]:
item1_price = 75365.7
item2_price = 5365.3
print(f"Item 1 price : ${item1_price} only")
print(f"Item 2 price : ${item2_price} only")
Item 1 price : $75365.7 only
Item 2 price : $5365.3 only

With width as 15.

In [2]:
item1_price = 75365.7
item2_price = 5365.3
print(f"Item 1 price : ${item1_price:15} only")
print(f"Item 2 price : ${item2_price:15} only")
Item 1 price : $        75365.7 only
Item 2 price : $         5365.3 only

Formatting alignment

For the alignment of the numbers, we can use < (for left-aligned), > (for right-aligned), and ^ (for center-aligned) after the colon(:).

Example:

print(f"Item 1 price : ${item1_price:<15,} only")

Left aligned with width 15.

In [1]:
item1_price = 75365.7
item2_price = 5365.3
print(f"Item 1 price : ${item1_price:<15,} only")
print(f"Item 2 price : ${item2_price:<15,} only")
Item 1 price : $75,365.7        only
Item 2 price : $5,365.3         only

Right aligned with width 15.

In [2]:
item1_price = 75365.7
item2_price = 5365.3
print(f"Item 1 price : ${item1_price:>15,} only")
print(f"Item 2 price : ${item2_price:>15,} only")
Item 1 price : $       75,365.7 only
Item 2 price : $        5,365.3 only

Center aligned with width 15.

In [3]:
item1_price = 75365.7
item2_price = 5365.3
print(f"Item 1 price : ${item1_price:^15,} only")
print(f"Item 2 price : ${item2_price:^15,} only")
Item 1 price : $   75,365.7     only
Item 2 price : $    5,365.3     only

In case we want dollar sign to appear with the price.

In [4]:
item1_price = 75365.7
item2_price = 5365.3
format_item1_price = "$" + f"{item1_price:,}"
format_item2_price = "$" + f"{item2_price:,}"
print(f"Item 1 price : {format_item1_price:^15} only")
print(f"Item 2 price : {format_item2_price:^15} only")
Item 1 price :    $75,365.7    only
Item 2 price :    $5,365.3     only

References

  1. Format strings and numbers with format() in Python
  2. Python Number Format
  3. Python Format Numbers