Learn all about the print function in Python, a very simple but widely used feature in Python.
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
To receive the file(s) used in the class by email, fill in :
Your best email
To send
Python Icon
Python
Impressive
You will learn the fastest growing programming language in the world to create incredible automations, develop websites, perform data analysis, work with Data Science, Artificial Intelligence, even if you have never had any contact with Programming in your life.
Start now
Right arrow
Python icon used as backgroundThree images of Python course classes
Print in Python – How to Customize Prints
In today's lesson, I want to talk to you about the print function in Python. This is a very simple feature, but it is widely used to display information.
You've probably already used print in your codes because it's a basic Python structure. However, in this lesson, I want to show you how you can customize print in Python.
That's right! There are different parameters that we can define part time data for the print, modifying its return and customizing how it displays the information we are passing.
I will teach you the standard use of print, how to format the print, use separators, pass variables within it and how to apply the end parameter. And at the end, I will also show you how to build a timer with Python using print.
So, download the available material and come with me to learn more about the print function in Python.
Default Print Usage
The print function is used to print information on the screen, either in the terminal or in the console. This is a standard Python function , meaning we don't need to import any module or library to use it.
The print syntax is very simple and you have probably already used this function in your code. We just need to define the argument, the value, that we want to display in parentheses. This value can be a text, a number, a variable and so on.
Warning: Avoid assigning values to the print() function. A very common mistake, especially among those who are starting to study Python, is to use print followed by an equal sign .
By doing this, you will override the default behavior of the function by assigning it a new value. This will cause print to no longer work correctly in your code and will start to display errors.
Print of Various Information
The print function is not limited to displaying a single piece of information at a time. We can use it to display multiple pieces of information at the same time, simply by separating them with commas. For example:
In this case, we pass three different pieces of information to the print in Python, including a variable.
It is important to note that when passing numbers or variables to the print function, it is not necessary to enclose them in quotes. Quotes are only used for values that are already, by default, a text, a Python string .
Also, note that, by default, the print adds a space between one piece of information and another.
Formatting Values in Print
When working with print, you may want to format the displayed values, adjusting the decimal places, defining the thousands separator, displaying the values as a percentage, among other possible edits.
However, the print function does not inherently have a specific feature to do this. To do this, we will need to use f-strings , passing a lowercase f before the quotes and using curly braces to surround the variables that we want to insert into the text.
Print in Python – How to Customize Prints
-
- Posts: 93
- Joined: Sun Dec 15, 2024 5:00 am