Print() in Python

A guide to printing and formatting your scripts output.

We all start here:

If you want to separate the output into different lines, just use \n

Numbers require you to cast them as strings :

And of course you can wrap everything in a function and print the output:

Printing is also a very handy way to inspect variables and objects:

Objects/classes are slightly more complex because you need to differentiate between the class itself and the instance/ instance method :

These I think are some common basics that will cover most of your day to day, but printing can get better and become even more useful with some extra effort and know how…

Probably the first roadblock one encounters when printing is concatenating multiple variables along with some text, this can easily be done ( in fact we already did a couple of examples ago ) by separating things with a + sign:

This could be considered a simple way of formatting your output, but it is by no means the only one, the following section deals with Python’s formatting function :

Understanding Python’s formatter is half the battle, so let’s start with the basic template:

The utility of the format() function comes (primarily) when adding more than one value:

Variables get added in the order the {} are added, so our revised template could be:

You can add as many variables as you need ( here 3 for simplicity ):

If keeping track of your replacement variables is proving difficult or you need to replace the order you can use index numbers as so:

And how about formatting ? If you notice we haven’t had to convert to strings our numbers, and that is another benefit of using format() , yet this is only the beginning, you can format to your hearts content by using a special syntax, here’s a few common examples:

This subject is somehow deep, so I encourage you to consult the documentation for a look at what else you can format (dates, tables, hex values and on and on ):

A third way of formatting and fast becoming the norm are f-strings, which if you have been following along will be easy to comprehend since they are basically a shorter form of format() , in a nutshell:

let’s rewrite the previous format()examples:

More values no problem:

Revised template:

Using them with functions:

And of course you can format everything like with format() :

Like with format() , using and mastering f-Strings is a question of practice, they are relatively new, so use them if your environment allows it.

If you were to expand your knowledge on formatting and couple it with other python idioms and functions, you could do something like this:

So in one sense you are halfway there, the rest is adding new knowledge to the basics presented here and elsewhere.

Another route is to use a library to supplement your printing needs, there are a few out there for tabular data, here’s one example:

I guess the final frontier of printing is moving away from the terminal and into the real world, for that you will need to export whatever you are printing into a document format like a pdf, here’s a barebones example that uses tabulate:


If you want to follow this route, the next step might be to add html formatting and templating, (jinja for instance):

Also check other reporting libraries:

Most likely you will be using print() a lot while coding in python, and as such I hope you now have some extra insight into the different ways and cases you can use it, from printing for quick inspection, formatting complex printouts and even some basic reports.

Happy printing and thanks for reading !

AI, Software Developer, Designer :

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store