In this lesson, we will study about various ways for Python print format, through which we can print our data on the console and interpolate it.
Python print format
Easy print formatting sounds a small feature in any language but is one of the most used ones in daily programs. If a language supports easy print formatting, this will be a heaven for programmers! Let’s go on and study some examples for formatting.
Please note that all the examples were tested on Python 3.6. Some of these might not work in Python 2.x as these were only introduced with Python 3.
Python print formatting examples
Let’s start our journey with simple examples and building upon the slowly.
Python printing multiple values
A simple example will be to print multiple values. Let’s see how this is done:
1 2 3 4 5 6 |
>>> a = 1 >>> b = 1.2 >>> c = "Python" >>> print(a, b, c) |
We will obtain the following result when we run this script:
Separating multiple values in print
In the last script, we saw how we can print multiple values. Here is a little modification to it:
1 2 3 4 5 6 |
>>> a = 1 >>> b = 1.2 >>> c = "Python" >>> print(a, b, c, sep=",") |
We will obtain the following result when we run this script:
That looks cleaner, right?
Python print format value interpolation
We can very easily interpolate values of any type in our print formats. Let’s see how this is done:
1 2 3 4 5 |
name = "Shubham" best_place = "JournalDev" print("My name is {} and best place to study programming is {}".format(name, best_place)) |
The output is clean:
There are more ways to do this. Here is a little modification to the script:
1 2 3 4 5 |
name = "Shubham" best_place = "JournalDev" print("My name is {0} and best place to study programming is {1}".format(name, best_place)) |
The print format output will remain same like the last program.
Actually, we can even modify the order of the values in format tuple as:
1 2 3 4 5 |
name = "Shubham" best_place = "JournalDev" print("Best place to study programming is {1}, my name is {0}".format(name, best_place)) |
The output is the same:
Value Alignment
We can even apply alignment to our outputs. Let’s see some examples here to center align our output:
1 2 3 4 |
name="{:^20}".format('journaldev') print(name) |
The output will be:
If you run the program yourself, you will notice that as the string journaldev
is actually 10 characters long, there 5 gaps before and 5 gaps after the String. Also, 20
decides the total length of the output including the String.
Signed Numbers
We can also print numbers with a signed value. Let’s see some examples here:
1 2 3 |
print('{:+d}'.format(42)) |
Output will be:
Dictionary Formatting
We can also format python dictionary values. Let’s see some examples here:
1 2 3 4 |
stark = {'first': 'Ned', 'second': 'Brandon', 'third': 'Rob'} print('{first} {third}'.format(**stark)) |
The output will be:
Datetime Formatting
We can also format Datetime values. Let’s see some code snippets here:
1 2 3 4 |
from datetime import datetime print('{:%Y-%m-%d %H:%M}'.format(datetime(2017, 12, 7, 13, 22))) |
The output will be:
This allows us to format our DateTime values inline. This formatting option wasn’t available prior Python 2.6.
We can try another formatting option with DateTime with which we can provide separate options for date and time:
1 2 3 4 5 |
from datetime import datetime myDate = datetime(2017, 12, 7, 13, 22) print('{:{dfmt} {tfmt}}'.format(myDate, dfmt="%Y-%m-%d", tfmt="%H:%M")) |
The output will be the same as in previous image.
Decimal Formatting
We can also format Decimal values up to a precision point. Let’s see some code snippets here:
1 2 3 4 |
value="{:{width}.{prec}f}".format(3.1428, width=5, prec=2) print(value) |
The output will be:
We can try even without providing a width of course when we’re unsure of the answer.
Python print formatting summary
In this lesson on print formatting in Python, we saw how we can format our values in common ways. Use them to beautify the output.
Reference: API Doc