Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
Image de Guide to the print() function in Python - Interactive Tutorial
Python

Guide to the print() function in Python - Interactive Tutorial

Photo de Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Published on 6 janvier 2025 · 3 min of reading

Table of content

Share with

In the world of Python programming, the print() function is undoubtedly one of the most used and essential for both beginners and advanced users. It allows displaying information on the screen, thus facilitating debugging and interaction with the user. This tutorial aims to guide you through the basic and advanced features of print(), so you can harness its full potential in your projects.

Why is the print() function important?

The print() function plays a crucial role by allowing developers to check the state of variables, track the program's execution flow, and display error or information messages for the user. Whether you are developing a simple application or a complex system, knowing how to use print() effectively can greatly enhance your development process.

What you will learn

In this article, we will first explore the basics of displaying with print(), then we will dive into advanced techniques for customizing and styling your outputs. Through practical examples and detailed explanations, you will be able to apply these concepts to your own Python projects.

Display in brief

The print() function in Python is one of the most used for displaying outputs on the screen. It is essential for debugging scripts or simply interacting with the user. In this section, we will explore how to use print() effectively, as well as some of its advanced features.

Basic syntax

The basic syntax of the print() function is simple. Here is a classic example:
python
This will display "Hello, World!" on the console. You can pass multiple arguments to print(), which will be separated by a space by default:
python
Result: Hello, World! Welcome to Python.

Customizing the separator

The sep parameter of print() allows you to change the default separator between arguments:
python
Result: Python-is-great

Custom line ending

By default, print() ends each call with a newline. You can change this behavior with the end parameter:
python
Result: This text is on the same line as this one.

Displaying variables

print() is also used to display the value of variables. For example:
python
Result: Name: Alice Age: 30

Advanced formatting with f-strings

Introduced in Python 3.6, f-strings offer a more intuitive way to format strings:
python
Result: Name: Alice, Age: 30

Displaying complex data

print() can also display complex data structures like lists or dictionaries:
python
Result: Fruit list: ['apple', 'banana', 'cherry']
For dictionaries:
python
Result: Person details: {'name': 'Alice', 'age': 30}

Intermediate conclusion

The print() function is much more than a simple tool for displaying text. By understanding its parameters and leveraging features like f-strings, you can make your outputs clearer and more informative. This is particularly useful for debugging and presenting results in a readable and professional manner.

Displaying with style

To go beyond simple text display, Python offers ways to style your outputs to be not only informative but also aesthetically pleasing. Let’s explore some techniques to add style to your prints.

Using colors

Color display can make your outputs more attractive and help easily differentiate different parts of the text. Although print() has no built-in support for colors, you can use external libraries like colorama:
apl
Then, in your Python code:
python
This approach allows highlighting error messages in red or success messages in green, for example.

Formatting with str.format()

Before f-strings, str.format() was a popular method for formatting strings:
python
This allows you to insert dynamic values into a string using curly braces as placeholders.

Alignment and width

When working with data tables or wanting to align text, you can specify the width and alignment in your formatted strings:
python
This aligns the text to the left and reserves 10 characters for each column.

Using special characters

Special characters like tabs ( ) and newlines ( ) can be used to structure text:
python
This displays information in a structured way, useful for simple reports.

Displaying tables

To print tables clearly, you can use libraries like tabulate:
apl
And in your code:
python
This produces well-formatted tables that are easy to read and interpret.
These techniques will allow you to create more professional outputs, ideal for reports or command-line interfaces. By using colors, formatting, and structuring, your prints can become a powerful tool for communication and data presentation.

Want to go further?

This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Become a Data Analyst
Become a Data Analyst
6 months
Intermediate
Guarantee