Python print format With Examples

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:

We will obtain the following result when we run this script:
Python Print format multiple values

Separating multiple values in print

In the last script, we saw how we can print multiple values. Here is a little modification to it:

We will obtain the following result when we run this script:
Python Print formatting with Separator

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:

The output is clean:

python print format interpolate

There are more ways to do this. Here is a little modification to the script:

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:

The output is the same:

python print format interpolate

Value Alignment

We can even apply alignment to our outputs. Let’s see some examples here to center align our output:

The output will be:

python print format interpolate

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:

Output will be:

python print format interpolate

Dictionary Formatting

We can also format python dictionary values. Let’s see some examples here:

The output will be:

python print format interpolate

Datetime Formatting

We can also format Datetime values. Let’s see some code snippets here:

The output will be:

python print format interpolate

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:

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:

The output will be:
python print format interpolate
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

By admin

Leave a Reply

%d bloggers like this: