Print() in Python

A guide to printing and formatting your scripts output.

Why read this ?


We all start here:

print(‘Hello World’)

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

print('Hello \nWorld')

Numbers require you to cast them as strings :

print ('this is line: ' + str(1))
print ('this is line: ' + str(2))

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

def gDay():    
return ('Good Day Sir !')

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

Using a list, but could be a Tuple,Dictionary,or any iterable.

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

class simpleObject():
def simpleObject_method(self):
return ('Hello again')

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…

Intermediate Printology :

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:

var_1 = 'This is number: '
numbers = [1,2,3]

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 :

#Same example using a formatter, don't worry if it doesn’t make much sense yet, just note that the output is the same as above:

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

text = 'Hello there'

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

text_1 = 'Hello there'
text_2 = 'General Kenobi'
print("{}...{}".format(text_1, text_2))

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 ):

text_1 = 'Hello Henchman'
text_2 = 'Oddjob'
henchmanRank = [1,2,3,4,5]

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

firstName =  'James'
lastName = 'Bond'

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:

# Add a coma :

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 ):

⚠️ Note: There is an older syntax for formatting strings % along with a newer one we will cover next, I focused so far on the intermediate one.


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:

text = 'Hello there'

More values no problem:

text_1 = 'Hello there'
text_2 = 'General Kenobi'

Revised template:

text_1 = 'Hello Henchman'
text_2 = 'Oddjob'
henchmanRank = [1,2,3,4,5]

Using them with functions:

def to_uppercase(text):
return text.upper()

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

# Add a coma :

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.

A second opinion Doc ? Couple of tutorials on fstrings you might also like :

Becoming a master printologist ( Dr. printer, I presume )

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

results = [10.22111, 30.33, 40.69999]

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:

Using Tabulate:

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:

Setup: You need to install a binary/executable (wkhtmltopdf) and a package/library (Pdfkit).


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.

tl;dr: Use print() for inspections and small things, format() and f-strings for complex readouts and libraries/complex formatters for advanced and exportable printouts and 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