How to print Integer values in Python
Printing integer values is a basic yet essential operation in Python.
This guide explores various methods for displaying integers, whether as standalone values, within strings, or as part of formatted output.
Printing Integers Directly
The most straightforward method is to pass the integer directly to the print()
function:
my_int = 1234567
print(my_int) # Output: 1234567
print(100) # Output: 100
print(200) # Output: 200
This displays the integer to the console as is.
Printing Integers Using f-strings
Formatted string literals (f-strings) provide a concise and readable way to embed integers within strings:
my_int = 1234567
result = f'The number is {my_int}'
print(result) # Output: The number is 1234567
f-strings automatically handle the conversion of integers to strings when included within curly braces.
Concatenating Strings and Integers with +
To combine a string and an integer using the addition operator (+), you must explicitly convert the integer to a string using str()
:
my_int = 100
result = 'The number is ' + str(my_int)
print(result) # Output: The number is 100
Attempting to directly concatenate a string and an integer with +
will cause a TypeError
. Always use str()
to convert integers to strings when concatenating them with strings using the +
operator.
Converting Strings to Integers with int()
If you have an integer stored as a string, use the int()
constructor to convert it to an integer before printing or using it in numerical calculations:
my_str = '1234567'
my_int = int(my_str)
print(my_int) # Output: 1234567
print(int('105')) # Output: 105
print(int('5000')) # Output: 5000
The int()
constructor can parse strings containing numbers into integer objects.
Printing with sys.stdout.write()
The sys.stdout.write()
method provides a lower-level way to output text to the console. It requires strings as input so you'll need to convert any integers to string first using str()
:
import sys
my_int = 123456
sys.stdout.write('The number is ' + str(my_int)) # Output: The number is 123456
While print()
is a higher-level function that internally uses sys.stdout.write()
, the latter does not automatically add a newline character. You have to append a newline using \n
character if needed. sys.stdout.write()
also doesn't automatically convert non-string types like print()
does.
Printing with Multiple Comma-Separated Arguments
The print()
function can take multiple, comma-separated arguments, automatically separating them with a space (by default):
my_int = 100
print('The number is ', my_int) # Output: The number is 100
print('The number is ', my_int, sep='') # Output: The number is 100
- You can modify the default separator using the
sep
keyword argument, e.g.sep = ''
to remove the default space or add a custom string separator.
Formatting Integers with str.format()
The str.format()
method offers another flexible way to format strings including integers. You specify replacement fields within the string using curly braces .
my_int = 247
result = "The integer is {}".format(my_int)
print(result) # Output: The integer is 247
first = 'Tom'
last = 'Nolan'
result_name = "His name is {} {}".format(first, last)
print(result_name) # Output: His name is Tom Nolan
The str.format()
method handles type conversion of integers automatically, and allows you to specify the order that the arguments should be inserted using their indexes "{1} {0}".format(first, last)
.
Make sure that you provide as many arguments as replacement fields in the string. If the number of arguments and placeholders doesn't match it will cause an error.
Checking Variable Types
If you're uncertain about the type of a variable, use type()
or isinstance()
for verification:
my_int = 123
print(type(my_int)) # Output: <class 'int'>
print(isinstance(my_int, int)) # Output: True
my_str = 'hello'
print(type(my_str)) # Output: <class 'str'>
print(isinstance(my_str, str)) # Output: True
- The
type()
function reveals the type of a variable (e.g.,<class 'int'>
). - The
isinstance()
function tests if an object belongs to a given class or subclass, returningTrue
orFalse
.