Advanced print() Function Tutorial and Techniques for Python Developers

Python is the most popular programming language which is widely used for a variety of applications, from Web Development to Scientific Computing, Artificial Intelligence, Machine Learning to Quantum Computing, and Data Science. What makes it so attractive for students, data scientists and programmers is its simplicity, readability, and flexibility, and of course, its large and active community contributes to its growth and development.


print("Hello World!")

One of the most commonly used functions of Python is the print() function, which allows developers to output text to the console. The print() function may seem simple at first glance, but there are many advanced techniques and features that can help developers get the most out of it.


In this tutorial, we will explore advanced techniques and features of the print() function in Python in depth. We will cover different ways to format output, advanced usage of the function, tips for debugging with print(), common mistakes to avoid, and alternative print functions available in Python

Table of Contents

  1. What is print() in Python?
  2. Understanding the syntax of print() function
  3. Deep-dive into print function Parameters with Examples
  4. Examples with a combination of all parameters of print function
  5. Advanced formatting using the print function.
  6. Introduction: f-strings with print function
  7. Common Errors/Exception with print function
  8. More advanced usage of print function with python modules


What is print() in Python?




Deep Dive into print function Parameters with Examples


Examples with a combination of all parameters of print function


Introduction: f-strings

String interpolation using f-strings

Formatting of variables and expressions within f-strings


f-strings with dictionaries example


Nested f-strings example

name = "Andrew"
age = 19
city = "New York"

message = f"My name is {name} and I am {age} years old. I live in {city}."
nested_message = f"Here's my info: {message}"
print(nested_message)

Here's my info: My name is Andrew and I am 19 years old. I live in New York.


Common errors/exceptions and how to debug f-strings


Common Errors/Exception with print function


More advanced usage of print function with Python modules

Custom print function using functools module

Coloring the print fuction output

You can make use of the module termcolor (will need to pip install) to color code the print output in the console, you can do a lot of stuff here like.


Some of the features provided by the termcolor module:

Learn more:https://pypi.org/project/termcolor/

Progress bar using tqdm module

The tqdm module is a third-party library that provides an easy way to add progress bars to Python code. It is commonly used in loops or when iterating over large datasets to keep track of how much time is left until the task is completed.

Some of the features provided by the tqdm module:


Example: Progress status using print function

from tqdm import tqdm
import time

for i in tqdm(range(10)):
    print(f"Building AI Model {i}")
    time.sleep(1)

print("AI Model is ready to use!")
 0%|          | 0/4 [00:00<?, ?it/s] Building AI Model 0
 25%|██▌       | 1/4 [00:01<00:03,  1.00s/it] Building AI Model 1
 50%|█████     | 2/4 [00:02<00:02,  1.00s/it] Building AI Model 2
 75%|███████▌  | 3/4 [00:03<00:01,  1.00s/it] Building AI Model 3
100%|██████████| 4/4 [00:04<00:00,  1.00s/it] AI Model is ready to use!

Learn more:https://pypi.org/project/tqdm/


Summing what we learnt

  1. The print function is used to display output in the console.
  2. It takes one or more objects as input, which are separated by a comma by default.
  3. The separator can be changed using the sep parameter.
  4. The end character can be changed using the end parameter.
  5. The output can be redirected to a file using the file parameter.
  6. The flush parameter can be used to force the output to be immediately written to the console or file.
  7. f-strings are a new way to format strings in Python that allow you to embed expressions and variables inside curly braces {}.
  8. Nested f-strings can be used to format multiple variables or expressions in a single string.
  9. Conversion flags can be used inside f-strings to change how variables are displayed, such as using the !r flag to display a printable representation of an object.
  10. The logging module can be used for more advanced logging instead of using print statements.
  11. functools.partial can be used to create a customized print function with preset arguments.
  12. The termcolor module can be used to colorize text in the console.
  13. The progress bar can be created using the tqdm module to show the progress of long-running tasks.
  14. Errors can occur with the print function due to syntax, type, name, and Unicode encoding issues.
  15. Debugging techniques, such as using the repr function or printing variables, can be used to fix errors in the print function.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!