Python print() - Beginner-Friendly

The Python print() function is one of the first things we learn when learning the Python programming language. As in other programming languages, one of the first things you do is print the line Hello World! to the console. The same is true for Python.

But, there is more to the Python print() function than simply printing strings or numbers to the console with it. The Python print() function is also used for debugging, among other things. In this article, you will learn everything you need to know about the Python print() function.

python print

ℹī¸ This article has a corresponding Replit repository that you can fork and play around with. To use it, create a free Replit account and click on Fork Repl after opening the link below. This is the best way to practice programming!

Table of Contents

🐍 Learn Python Programming
🔨 Python Basics
👉 Python Syntax
👉 Python Variables
👉 Python print()
⚙ī¸ Python Specifics
👉 Python List Replace - Simplified
👉 Create a GUI in Python

What is the Python print() Function?

The Python print() function is able to print values to the console. But why do we need this functionality? There is a multitude of use-cases for the print() function. You can print strings, numbers, boolean values, tuples, objects, and other things to the console.

The print() function in a Python context, is mostly used for debugging but is not limited to this functionality. It is often used in courses and tutorials to build text-based games or exercises to better understand and learn the language.

Python print() Syntax

The most basic way of using the print() function is by printing a string to the console:

print("Hello World!")
Code language: PHP (php)

Output:

Hello World!
Code language: plaintext (plaintext)

Python print() Syntax Breakdown

The full print() syntax according to the Python documentation looks like this:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Code language: PHP (php)

The following parameters can be used:

  • objects - Any object you would like to print. Converted to a string before it is printed.
  • sep - Optional: A separator with the default value of ' ' (a space) to separate printed objects.
  • end - Optional: This allows you to specify what to print at the end of the print statement. The default is a newline character '\n'.
  • file - Optional: This allows you to print an object that has a write method. The default is sys.stdout.
  • flush - Optional: A boolean value that defines if the output is flushed True or buffered False. The default value is False.

While all of this is nice to know, you mostly will not need any of these parameters except for debugging purposes.

More Python print() Examples

To give you a better idea of how the print() function works, let's look at some more real-world examples. We already learned how to print "Hello World" to the console, as the most basic form of using the print() statement.

Printing Python Lists

We are able to print lists using the print() function. For this, we must first create a list and then print it to the console:

our_list = ["Python", "JavaScript", "Golang"] print(our_list)
Code language: PHP (php)

What do you think will be printed? Only the string values or the entire list?

Output:

['Python', 'JavaScript', 'Golang']
Code language: plaintext (plaintext)

Naturally, the entire list will be printed. To be able to print out separate values of a list, we need to use a loop.

Printing a String using a Separator

We can use the sep parameter we have learned about earlier to separate two strings:

print("Hi!", "How was your day so far?", sep="+++")
Code language: PHP (php)

Output:

Hi!+++How was your day so far?

The separator will be inserted at the location of the comma , after the string "Hi!". This can be useful in some cases.

Printing Using the File Parameter

Another parameter we can use is the file parameter. This allows us to open certain files in writing mode. In this example, we attempt to open a file called file.txt using our code. Of the file does not exist, Python will create it for us and opens it in writing mode:

our_file = open("file.txt", "w") print("This is the content of our file.txt file!", file = our_file) our_file.close()
Code language: PHP (php)

The file.txt file should have been created on your system with the content of our print() statement.

Note: This even works on Replit. You have to open the Shell and type:
cat file.txt
Python Print Example

Debugging with print()

Let's look at some examples of how to use Python print() for debugging purposes. Using the print() function for debugging is only one way of debugging your Python code. Using print() to debug your code is also known as print debugging and it is the most basic form of debugging.

To find errors in your code using the print() function, you basically want to include print() statements throughout your code, or in specific places where you suspect the error happens. This helps you to easily identify wrong or falsy results when running your code.

Let's assume we want to print a string if number2 is larger than number1, and we have mixed up our numbers:

number1 = 5 number2 = 3 if number2 > number1: print("number2 is definitely larger than number1!")
Code language: PHP (php)

If we run our code, we actually don't get any output, because number2 is in fact smaller than number1. Now just assume this is a more complicated piece of code with potentially many more lines. The easiest approach would be to include print() statements underneath of all the potential variables that our code requires to run:

number1 = 5 print("number1 = ", number1) number2 = 3 print("number2 = ", number2) if number2 > number1: print("number2 is definitely larger than number1!")
Code language: PHP (php)

Looking at the output of this code, we would clearly see that number1 is in fact larger than number2.

Output:

number1 = 5 number2 = 3

This example should give you a pretty good idea of how we can use the print() function for debugging purposes.

Python print() Summary

These are just a few examples of the power of the Python print() function. You should definitely internalize this function as you will certainly need it for debugging your code at some point. It is a powerful tool to go through each line of your code and analyze it for errors.

If you want to learn more about Python in a beginner-friendly way, make sure to bookmark our Python Tutorials section or check out some of the related tutorials below!

🐍 Learn Python Programming
🔨 Python Basics
👉 Python Syntax
👉 Python Variables
👉 Python print()
⚙ī¸ Python Specifics
👉 Python List Replace - Simplified
👉 Create a GUI in Python

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap