How to Print Integer Values in Python?

In Python, data is represented by different data types such as int, float, string, etc., as per its(data) category. To represent the integer/whole number without decimal points, the “int()” function is used in Python. There are multiple functions, such as print(), f-string, etc., that can be used to print these data types.

This Python write-up will provide a detailed guide on printing integer values. Here is the content of the guide:

So, let’s get started!

Method 1: Using print() Function to Print Integers

The simplest way to print integer values is using the “print()” function. Here is an example:

Code:

int_value = 1947

print(int_value)

print(4598)

In the above code, the “integer” value is initialized and stored in a variable named “int_value”. The first print() function accepts the integer variable as an argument and displays it on screen. Similarly, in the second “print()” function, the integer value is directly passed as an argument, and the function returns the value on screen.

Output:

The above output verified that the integer value had been successfully printed.

Method 2: Using the f-string Method to Print Integer Inside the String

The integer value is also printed inside the string using the “f-string” method. The “f-string” method uses curly braces inside the string with an integer variable name as a placeholder. Here is an example:

Code:

int_value = 1947

print(f'Input number is {int_value}')

In the above code, the integer value is placed inside the string using the “f-string” method.

Output:

The above output successfully placed the integer value inside the string.

Method 3: Using Built-in int() Function to Convert Value Into Integer

The built-in “int()” function is utilized in Python to convert the numeric string value into an integer. The converted value will be printed on the console screen using the “print()” function. Here is an example:

Code:

numeric_string = '1687'

print(int(numeric_string))

In the above code, the numeric string value “1687” is converted into an integer using the built-in “int()” function. The “print()” function is then used to display the “integer” value on the console output.

Output:

The above output shows that the integer value has been successfully printed.

Method 4: Using sys.stdout.write() Function

To print integer values in Python, the “sys.stdout.write()” function of the “sys” module is utilized in the following example code.

Code:

import sys
 
int_value = 6575
sys.stdout.write("Input number is: " + str(int_value))

In the above code, the “sys.stdout.write()” function accepts the input variable value and returns it on screen. The “str()” function is utilized to convert the int value into a string because this function will return TypeError if the integer value is directly placed.

Output:

The above output verified that the integer value had been printed.

Conclusion

To print integer values, the “print()” function, “f-string” method, and “sys.stdout.write()” function of the “sys” module is used in Python. The “int()” built-in function is also used to convert the value into an integer and display it on the screen. All the methods mentioned here are briefly explained with examples to print integer values in Python.