Python Output Formatting

In this tutorial, we will learn to format the output. Formatting and output refer to presenting the output of a program. We can format the output in the human-readable form or write the data into a file and some other specified form. Sometimes we need to control the coming output so we can format it according to our requirements. Users can handle strings by using string slicing and concatenation operations to create any layout.

There are various ways to format output.

  • We can use the f string method to format the string literals.
  • The format() method of strings helps a user create a fancier output.
  • We can also perform the string concatenation operations to create any layout we want. The string class also has some methods that perform useful operations for padding strings to given column width.

Formatting Output using String Modulo Operator (%)

The % operator helps us to format the string. It interprets the left argument like printf() format string applied to the right argument. Python doesn't provide the printf() function. For this purpose, the modulo operator is overloaded by the string class to perform string formatting. It is also known as the String Modulo operator.

The string modulo operator is still available in Python (3.x), and it is widely used, but nowadays, the old formatting style is removed from the language.

Let's understand the following example -

Example -

Output:

integer :  1, float :  5.33
Total Player :  24, Batsman :  12
  062
  4.56E+02

Explanation -

In the above code, "%2d" is a placeholder that uses the first element of the tuple. It prints the two characters. For the format description of the float number, "%5.2f" is used. It is also introduced with character % like other placeholders. It shows the total number of strings it can contain. The decimal part of the number or the precision is set to 2, the number following "." in the placeholder. The last character, "f" shows a float number.

Formatting Output Using the Format Method

The format() method takes more manual effort than other methods. We use {} to mark the substitution of variables and provide detailed formatting directives, but we also need to provide the formatted information. We can use the number in the brackets as positional formatting. Let's understand the following example.

Example -

Output:

Hello World
Welcome to JavaTpoint
JavaTpoint to Welcome

The brackets and characters within the item are called format fields which are replaced with the object passed into the format() method. A number in the brackets can be used to refer to the object's position passed into the format() method.

Let's understand another example -

Example -

Output:

Number one position is Java, T, and Point.
Java :12, Tpoint :    0.55
Second argument:  11, first one:   47.42
Java:   453,  Tpoint:    59.06

Example - 3:

Output:

Java: 4127; For: 4098; Java: 4127
I love JavaTpoint Website

Formatting output using the String Method

We can also format the output using the string slicing and concatenation operations. The string type has some methods that help format output in a fancier way. Few methods which helps to formatting an output - str.ljust(), str.rjust(), and str.centre(). Let's understand the following example -

Example -

Output:

Center aligned string with fillchr: 
$$$$$$I love JavaTpoint$$$$$$$
The left aligned string is: 
I love JavaTpoint&&&&&&&&&&&&&&&&&&&&&&&
The right aligned string is : 
-----------------------I love JavaTpoint

Format Conversion Rule in Python

Below is the table of the conversion and its meaning.

ConversionMeaning
dIt represents a Signed integer decimal.
iIt represents a Signed integer decimal.
oIt represents Unsigned octal.
uIt represents Obsolete and equivalent to'd', i.e. signed integer decimal.
xIt represents an Unsigned hexadecimal (lowercase).
XIt represents an Unsigned hexadecimal (uppercase).
eIt represents Floating point exponential format (lowercase).
EIt represents Floating point exponential format (uppercase).
fIt represents a Floating point decimal format.
FIt represents a Floating point decimal format.
gIt represents Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise.
GIt represents Same as "E" if exponent is greater than -4 or less than precision, "F" otherwise.
cIt represents Single character (accepts integer or single character string).
rIt represents String (converts any python object using repr()).
sIt represents String (converts any python object using str()).
%It represents No argument is converted, results in a "%" character in the result.

Conclusion

We have discussed how to format the output in Python. We have described the various techniques along with examples. Python provides the facility to modify the output according to the requirements. Users can use string or format() methods with positional arguments. This tutorial also included the conversion rules table.






Latest Courses