How to print Integer values in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Print integer values in Python
  2. Print an integer using a formatted string literal
  3. Print a string and an integer together using addition (+) operator
  4. Print integer values using the int() class
  5. Print integer values using sys.stdout.write()
  6. Print a string and an integer together using commas
  7. Print a string and an integer using str.format()

# 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).

main.py
my_int = 1234567 # โœ… print integer print(my_int)

print integer using print function

The code for this article is available on GitHub

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.

main.py
print(100) # ๐Ÿ‘‰๏ธ 100 print(200) # ๐Ÿ‘‰๏ธ 200

# The print() function returns None

Note that the print() function returns None, so don't try to store the result of calling print in a variable.

main.py
my_int = 1234567 # โ›”๏ธ BAD (print always returns None) result = print(my_int) print(result) # ๐Ÿ‘‰๏ธ None

# Print an integer using a formatted string literal

You can also use a formatted string literal to print an integer.

main.py
my_int = 1234567 result = f'The number is {my_int}' print(result) # ๐Ÿ‘‰๏ธ The number is 1234567

print integer using f string

The code for this article is available on GitHub
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
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}.

# Print a string and an integer together using addition (+) operator

To print a string and an integer together:

  1. Use the str() class to convert the integer to a string.
  2. Use the addition (+) operator to concatenate the two strings.
  3. Use the print() function to print the result.
main.py
my_int = 100 result = 'The number is ' + str(my_int) print(result) # ๐Ÿ‘‰๏ธ The number is 100

print integer using addition operator

The code for this article is available on GitHub

We used the str() class to convert the integer to a string.

This is necessary because the values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

If you try to use the addition (+) operator with a string and an integer, you'd get an error.

main.py
my_int = 100 # โ›”๏ธ TypeError result = 'The number is ' + my_int

To get around this, we have to use the str() class to convert the integer to a string.

When using formatted string literals, we don't have to explicitly convert the integer to a string because it's done for us automatically.

# Print integer values using the int() class

If your integer is wrapped in a string, use the int() class to convert it to an integer before printing it.

main.py
my_str = '1234567' my_int = int(my_str) print(my_int) # ๐Ÿ‘‰๏ธ 1234567

print integer value using int class

The code for this article is available on GitHub

We used the int() class to convert the integer to a string before printing it.

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

main.py
print(int('105')) # ๐Ÿ‘‰๏ธ 105 print(int('5000')) # ๐Ÿ‘‰๏ธ 5000

The constructor returns 0 if no arguments are given.

# Print integer values using sys.stdout.write()

You can also use the stdout.write() method from the sys module to print integer values.

main.py
import sys my_int = 123456 # ๐Ÿ‘‡๏ธ The number is 123456 sys.stdout.write('The number is ' + str(my_int))

print integer using stdout write

The print() function is actually a thin wrapper around sys.stdout.write.

The print() function uses the sys.stdout.write method under the hood by default.

Notice that we used the str() class to convert the integer to a string before using the addition (+) operator.

This is necessary because the values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

# Print a string and an integer together using commas

Alternatively, you can pass multiple, comma-separated arguments to the print() function.

main.py
my_int = 100 # ๐Ÿ‘‡๏ธ The number is 100 print('The number is ', my_int, sep='') # ๐Ÿ‘‡๏ธ The number is 100 print('The number is ', my_int)

print integer using commas

The code for this article is available on GitHub

We passed multiple, comma-separated arguments to the print() function to print a string and an integer.

You can use the sep keyword argument if you need to adjust the separator between the values.

By default, the sep argument is set to a space.

By setting the argument to an empty string, no extra whitespace is added between the values.

main.py
print('a', 'b', 'c', sep="_") # ๐Ÿ‘‰๏ธ "a_b_c" print('a', 'b', 'c', sep="_", end="!!!") # ๐Ÿ‘‰๏ธ "a_b_c!!!"

The string we passed for the end keyword argument is inserted at the end of the string.

The end argument is set to a newline character (\n) by default.

# Print a string and an integer using str.format()

This is a two-step process:

  1. Use the str.format() method to interpolate the variable in the string.
  2. Use the print() function to print the result.
main.py
my_int = 247 result = "The integer is {}".format(my_int) print(result) # ๐Ÿ‘‰๏ธ "The integer is 247"

print integer using str format

The str.format() method performs string formatting operations.

main.py
first = 'Bobby' last = 'Hadz' result = "His name is {} {}".format(first, last) print(result) # ๐Ÿ‘‰๏ธ "His name is Bobby Hadz"

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

The str.format() method takes care of automatically converting the integer to a string.

# Checking what type a variable stores

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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 code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev