How to print integer values in Python


Borislav Hadzhiev

Last updated: Aug 29, 2022


Photo from Unsplash

Print integer values in Python #

Use the print() function to print an integer value, e.g. print(my_int). If the value is not of type integer, use the int() class to convert it to an integer and print the result, e.g. int(my_str).
my_int = 1234567 # ✅ print integer print(my_int) # ----------------------------- # ✅ print integer value in a string print(f'The number is {my_int}') # ----------------------------- # ✅ convert string to integer print(int('567')) # ----------------------------- # ✅ pad integer with leading zeros my_int = 5 print(f'{my_int:02d}') # 👉️ 05 print(f'{my_int:03d}') # 👉️ 005

We used the print() function to print integer values.

The print function takes one or more objects and prints them to sys.stdout.

If you have an integer value, you can directly pass it to the print() function to print it.
print(100) # 👉️ 100 print(200) # 👉️ 200
Note that the print() function returns None, so don't try to store the result of calling print in a variable.
my_int = 1234567 # ⛔️ BAD (print always returns None) result = print(f'The number is {my_int}') print(result) # 👉️ None

Instead, store the value in a variable and pass the variable to the print() function.
my_int = 1234567 result = f'The number is {my_int}' print(result) # 👉️ The number is 1234567

We used a formatted string literal to print an integer value in a string.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
my_str = 'The number is:' my_int = 5000 result = f'{my_str} {my_int}' print(result) # 👉️ The number is: 5000

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

Alternatively, you can concatenate an integer to a string by converting the integer to a string and using the addition (+) operator.
my_int = 1234567 result = 'The number is ' + str(my_int) print(result) # 👉️ The number is 1234567
The values on the left and right-hand sides of the addition (+) operator have to be compatible types, so we converted the integer to a string.

If you aren't sure what type a variable stores, use the built-in type() class.
my_int = 123 print(type(my_int)) # 👉️ <class 'int'> print(isinstance(my_int, int)) # 👉️ True my_str = 'hello' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

If you need to convert a value to an integer and print the result, use the int() class.
print(int('105')) # 👉️ 105 print(int('5000')) # 👉️ 5000

The int class returns an integer object constructed from the provided number or string argument.

The constructor returns 0 if no arguments are given.

If you need to pad the integer with leading zeros to a certain length and print the result, use a formatted string literal
my_int = 5 print(f'{my_int:02d}') # 👉️ '05' print(f'{my_int:03d}') # 👉️ '005'

The first example pads the number with leading zeros to 2 digits and the second to 3 digits.

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.