String formatting with integers in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

String formatting with integers in Python #

Use a formatted string literal to format numbers to strings, e.g. result = f'The numbers are: {num1}, {num2}.'. Formatted string literals let us include expressions and variables inside of a string by prefixing the string with f.

main.py
num1 = 1 num2 = 2 num3 = 3 # ✅ string formatting with integers result = f'The numbers are: {num1}, {num2}, {num3}.' print(result) # 👉️ The numbers are: 1, 2, 3. # ----------------------------------------------------- # ✅ string formatting with integers set to fixed width hours = 9 minutes = 30 seconds = 5 result = f'The time is: {hours:02d}:{minutes:02d}:{seconds:02d} {"pm" if hours > 12 else "am"}' print(result) # 👉️ The time is: 09:30:05 am # ----------------------------------------------------- my_int = 5 result = f'{my_int:05d}' print(result) # 👉️ '00005'

The examples use formatted string literals to do string formatting with integers.

main.py
num1 = 1 num2 = 2 num3 = 3 result = f'The numbers are: {num1}, {num2}, {num3}.' print(result) # 👉️ The numbers are: 1, 2, 3.
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}.

The integers automatically get converted to a string when used in a formatted string literal.

This is quite convenient because if you use the addition (+) operator with a string and an integer, you'd get an error.

main.py
num1 = 1 # ⛔️ TypeError: can only concatenate str (not "int") to str result = 'The number is: ' + num1

The values on the left and right-hand sides of the addition (+) operator have to be of compatible types, so we have to use the str() class to convert the integer to a string.

main.py
num1 = 1 result = 'The number is: ' + str(num1) print(result) # 👉️ The number is: 1

This is not necessary when using f-strings because they convert the integers to strings for us.

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

main.py
hours = 9 minutes = 30 seconds = 5 result = f'The time is: {hours:02d}:{minutes:02d}:{seconds:02d} {"pm" if hours > 12 else "am"}' print(result) # 👉️ The time is: 09:30:05 am

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

main.py
my_int = 5 result = f'{my_int:03d}' print(result) # 👉️ '005' my_int = 100 result = f'{my_int:03d}' print(result) # 👉️ '100'

If you need to use conditionals when formatting strings and integers, use the ternary operator.

main.py
my_int = 50 result = f"Result: {1 if my_int > 100 else 5}" print(result) # 👉️ Result: 5

The ternary operator checks if the integer is greater than 100 and returns 1 if it is, otherwise the else block runs and 5 is returned.

If you use nested strings when formatting, make sure to alternate between single and double quotes.
main.py
my_int = 50 result = f"Result: {'a' if my_int > 100 else 'b'}" print(result) # 👉️ Result: b

Notice that the f-string is wrapped in double quotes and the strings in the expression use single quotes.

Had we wrapped the nested strings with double quotes, we would have terminated the f-string prematurely.

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.