Python Tutorial- Output Formatting

Python Exercise Lists

The print () function uses string modulo operator that begin with % to format and produce various types of data. See the table below for details.

String Modulo Operator Explanations
%d, %i Convert to decimal integer/ integer
%o Convert to octal integer
%x, %X Convert to hexadecimal integer
%e Convert to floating point (lowercase e)
%E Convert to floating point (uppercase e)
%f, %F Convert to decimal floating point
%g Smart selection using %f or %e format
%G Smart selection using %F or %E format
%c Formatting characters and their ASCII codes
%r Use the repr() function to convert an expression to a string
%s Use the str() function to convert an expression to a string

String modulo operator is just a placeholder, and it will be replaced by the value of the variables, constants, numbers, strings, addition, subtraction, multiplication and division.


age = 22
print ("Jason is% d years old!" % age)

The output is:

Jason is 22 years old!

The formatting string in the above example contains string modulo operator, %d which is a placeholder and eventually be replaced by the value of age variable. The % in the middle of sentence acts as delimiter, which is preceded by a formatted string and followed by the output result.

The formatting string can also contain multiple string modulo operator. At this time, multiple expressions must be provided in order to replace the corresponding string modulo operators. The multiple expressions must be enclosed in parentheses (), you can find the following example:

name = "Jason"
age = 22
place = "Secret Garden"
print ("% s is% d years old, he stays at % s."% (name, age, place))

The output is:

Jason is 22 years old, he stays at Secret Garden.

In short, the number of placeholder shall same as number of expressions which enclosed in parenthesis () and situated in the end of formatting string.

How to specify minimum output width in Python?

  • % 10d indicates that the integer width of the output is at least 10;
  • % 20s indicates that the output string width is at least 20.


a = 889977
print("a(10):%10d." % a)
print("a(5):%5d." % a)
name = "Free Learning Platform"
print("name(30):%35s." % name)
print("name(20):%20s." % name)

The output is:

a(10):   889977.
name(30):      Free Learning Platform.
name(20):Free Learning Platform.

It can be found from the results that for integers and strings, when the actual width of the data is less than the specified width, it will be filled in with spaces on the left; when the actual width of the data is greater than the specified width, it will be filled in according to the actual width of the data.

How to adjust the Alignment in Python?

By default, the data printed by print () is always right justified. That is, when the data is not wide enough, the data is always generate result on the right, and spaces are added on the left to reach the specified width. Python allows adding a flag before the minimum width to change the alignment. The flags supported by Python are as follows:

Flag Explanations
- Specify left alignment
+ The output numbers must always be signed; integers are +, and negative numbers are-.
0 Indicates that the extra width is filled with 0 instead of spaces.

A few things to take note:

  • For integers, when left-aligned is specified, 0 on the right side has no effect, because it will cause the changes of integer value.
  • For decimals, the above three flags can exist at the same time.
  • For strings, you can only use the (-) flag, because other symbols have no meaning for strings, and add in 0 changes the value of the string.

For example:

a = 88866
# % 08d means the minimum width is 8
print ("a (08):% 08d"% a)
#% + 8d means the minimum width is 8 with a symbol
print ("a (+8):% + 8d"% a)
b = 240.5
#%-+ 010d means the minimum width is 10, left-aligned, with symbols
print ("b (-+ 10):%-+ 010f"% b)
c = "Jason"
#% -10s means minimum width is 10, left justified
print ("c (-10):%-10s."% c)

The output is:


How to specify decimal precision of Python?

For decimals (floating point numbers), print () also allows you to specify the number of digits after the decimal point, that is, the precision of the decimals.

The precision value needs to be placed after the minimum width, separated by dots in the middle; you can also write the precision without writing the minimum width. The specific format is as follows:


.nf %

While as:

m is the minimum width,

n is the output accuracy,

. must exist.

For example:

e = 2.164192853
# Minimum width is 7, 3 digits are reserved after the decimal point
print ("% 7.3f"% e)
# The minimum width is 7, 3 digits are reserved after the decimal point and 0 are filled in the left.
print ("% 07.3f"% e)
# The minimum width is 7 and 3 digits are reserved after the decimal point and 0 are filled in the left which a (+) symbol.
print ("% + 07.3f"% e)

The output is:



More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises