Python print format

Filed Under: Python

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:


>>> a = 1
>>> b = 1.2
>>> c = "Python"
>>> print(a, b, c)

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:


>>> a = 1
>>> b = 1.2
>>> c = "Python"
>>> print(a, b, c, sep=",")

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:


name = "Shubham"
best_place = "JournalDev"
print("My name is {} and best place to study programming is {}".format(name, best_place))

The output is clean:
python print format interpolate

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


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:


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:
python print formatting interpolate ordering

Value Alignment

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


name = '{:^20}'.format('journaldev')
print(name)

The output will be:
python print formatting value center align

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:


print('{:+d}'.format(42))

Output will be:
python print format Signed Value Output

Dictionary Formatting

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


stark = {'first': 'Ned', 'second': 'Brandon', 'third': 'Rob'}
print('{first} {third}'.format(**stark))

The output will be:
Dictionary Formatting for python print

Datetime Formatting

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


from datetime import datetime
print('{:%Y-%m-%d %H:%M}'.format(datetime(2017, 12, 7, 13, 22)))

The output will be:
python print datetime format

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:


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:


value = '{:{width}.{prec}f}'.format(3.1428, width=5, prec=2)
print(value)

The output will be:
python print decimal formatting
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

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages