Formatting numbers to strings in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

Formatting numbers to strings in Python #

Use a formatted string literal to format a number to a string, e.g. result = f'{my_int:03d}'. The formatted string literal will format the number to a string of the specified width by left-filling it with zeros or spaces.

main.py
my_int = 14 result = f'The number is {my_int}' print(result) # 👉️ The number is 14 # ✅ format integer to string with width 3 (zero-padded) result = f'{my_int:03d}' print(result) # 👉️ '014' # ----------------------------------- # ✅ format integer to string (space-padded) result = f'{my_int:3}' print(repr(result)) # 👉️ ' 14' # ----------------------------------- my_float = 1.234567 # ✅ format float to string with width 8 with 3 decimal places result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235' # ✅ format float to a string with width of 4 with 3 decimal places result = f'{my_float:4.3f}' print(repr(result)) # 👉️ '1.235'

The first example uses a formatted string literal to format a number to a string.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
hours = 7 minutes = 47 seconds = 6 result = f'The time is: {hours:02d}:{minutes:02d}:{seconds:02d} {"pm" if hours > 12 else "am"}' print(result) # 👉️ The time is: 07:47:06 am

The :02d syntax is used to pad the integer with leading zeros to a fixed width of 2 digits.

You can use the same approach to format the number to a string with leading spaces.

main.py
my_int = 14 result = f'{my_int:3}' print(repr(result)) # 👉️ ' 14'

The example formats the number to a string with a width of 3.

If you just need to perform string formatting with numbers, include the variables in curly braces.

main.py
num1 = 2 num2 = 6 result = f'{num1} + {num2} = {num1 + num2}' print(result) # 👉️ '2 + 6 = 8'

You can use the same approach to format a floating-point number to a string.

main.py
my_float = 1.234567 # ✅ format float to string with width 8 with 3 decimal places result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235' # ✅ format float to a string with width of 4 with 3 decimal places result = f'{my_float:4.3f}' print(repr(result)) # 👉️ '1.235'

The number after the colon is the total width of the string.

The number after the period is the number of digits after the decimal.

You can also use the str.zfill() method if you need to format a number to a string with leading zeros.

The str.zfill method takes the width of the string and left-fills the string with 0 digits to make it of the specified width.

main.py
my_int = 14 result = str(my_int).zfill(3) print(result) # 👉️ '014' result = str(my_int).zfill(4) print(result) # 👉️ '0014'

Converting the number 14 to a string gives us a string with a length of 2.

Passing 3 as the width to the zfill() method means that the string will get left-filled with a single 0 digit.

The str.zfill() method handles a leading sign prefix (e.g. + or -) by inserting the padding after the sign.

main.py
my_int = -14 result = str(my_int).zfill(3) print(result) # 👉️ '-14' result = str(my_int).zfill(4) print(result) # 👉️ '-014'

Note that the sign counts towards the width of the string.

If the specified width is less than or equal to the length of the original string, then the original string is returned.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.