Skip to content

Python print integer format | Example code

  • by

Using the String modulo operator(%), you can easily format numbers (integer) and print in python.


# Old
print('%d' % (12,))

# New
print('{:d}'.format(42))

Example Python print format integer

The string modulo operator ( % ) is still available in Python(3.x) but nowadays the old style of formatting is removed from the language.

# print integer and float value
print("Number : %2d, Average : %5.2f" % (1, 015.333))

# print integer value
print("Total students : %3d, Boys : %2d" % (40, 20))

# print octal value
print("%7.3o" % (50))

# print exponential value
print("%10.3E" % (778.08957))

Output:

Python print integer format

Formatting integer (Number) using the format method

# combining positional and keyword arguments
print('Number {0}, {1}, and {other}.'
      .format(111, 222, other=333))

# using format() method with number
print("Number :{0:2d}, And :{1:8.2f}".
      format(2, 00.795))

# Changing positional argument
print("Second: {1:3d}, First: {0:7.2f}".
      format(47.42, 11))

print("Total: {a:5d},  Avg: {p:8.2f}".
      format(a=453, p=59.058))

Output:

Formatting integer (Number) using the format method

Do comment if you have any doubts and suggestions on this Python int topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading