Python · Code Helpers
Print
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Print
print()
is a built-in function that you already met! It's used to display output on the screen. You can use it to print strings, numbers, basically everything you want.
There are plenty of way to use print, especially to display variables. Let's see what they are!
Exercise 2 — Print and concatenation
The concatenation method is a way to combine multiple strings into one big string and then display it. In Python, we use
+
to do that.
This method is easy to use, but it can be a bit cumbersome when you have a lot of variables to concatenate, especially if not everything is a string, because you would need to cast all of them.
How to cast to string?
Casting a variable to a string is easy! You just need to use the
str()
function. Like this
str(variable)
.
codemon_name = "Aquachu"
codemon_level = 5
print("My Codémon name is " + codemon_name + " and his level is " + str(codemon_level))
Exercise 3 — Print and commas
Using print and commas is another way to combine variables and strings. But this time, you don't need to cast the variables to string!
This method is really easy and natural to use, you just need to separate the elements with a comma.
codemon_name = "Aquachu"
codemon_level = 5
print("My Codémon name is", codemon_name, "and his level is", codemon_level)
Exercise 4 — Print and .format()
Using print and .format() is another way to print variables.
This method is pretty logic, everytime you want to print a variable, you need to add a
{}
in the string. Then, you can use the
format()
method to replace the
{}
with the variable you want.
codemon_name = "Aquachu"
codemon_level = 5
print("My Codémon name is {} and his level is {}".format(codemon_name, codemon_level))
Exercise 5 — Print and f-string
The last method is the f-string. It is, in my opinion, the best way to print in Python with the comma method.
You just need to add an
f
before the string, and you can put the variables between
{
and
}
and use them directly in the string.
codemon_name = "Aquachu"
codemon_level = 5
print(f"My Codémon name is {codemon_name} and his level is {codemon_level}")
Exercise 6 — Print
Let's do a quick recap of the print function.
- concatenation method :
print("My Codémon name is " + str(codemon_name))
- commas method :
print("My Codémon name is ", codemon_name)
- format() method :
print("My Codémon name is {}".format(codemon_name))
- f-string method :
print(f"My Codémon name is {codemon_name}")
Exercise 7 — Which print method
What is the name of this print method?
print("My Codémon name is {}".format(codemon_name))
Options
• format() method
• commas method
• concatenation method
• f-string method
Hint : Remember: There are many ways to print a string in Python, commas, format(), f-string, and concatenation. Their names are pretty self-explanatory.
Exercise 8 — Which print method
What is the name of this print method?
print(f"My Codémon name is {codemon_name}")
Options
• f-string method
• format() method
• commas method
• concatenation method
Hint : Remember: There are many ways to print a string in Python, commas, format(), f-string, and concatenation. Their names are pretty self-explanatory.
Exercise 9 — Which print method
What is the name of this print method?
print("My Codémon name is ", codemon_name)
Options
• commas method
• f-string method
• format() method
• concatenation method
Hint : Remember: There are many ways to print a string in Python, commas, format(), f-string, and concatenation. Their names are pretty self-explanatory.
Exercise 10 — Which print method
What is the name of this print method?
print("My Codémon name is " + str(codemon_name))
Options
• concatenation method
• commas method
• f-string method
• format() method
Hint : Remember: There are many ways to print a string in Python, commas, format(), f-string, and concatenation. Their names are pretty self-explanatory.
Exercise 11 — Best Print Methods
Now you know different ways to print in Python. But which ones should you use?
In my opinion, the best methods are:
1. Using commas - Simple and clean
2. Using f-strings - Modern and flexible
name, level = "Pyrochu", 25
print("My Codémon", name, "is level", level)
print(f"My Codémon {name} is level {level}")
The other methods (string concatenation and .format()) work but are more constraining to use.
Back to the course