What is the print()
Function?
To explain briefly without confusing you too much, the print()
function is a function that allows us to output to the screen.
How to Use the print()
Function?
The print()
function has three different uses;
- Single quotes (
' '
) - Double quotes (
" "
) - Three quotes (
“”” “””
)
Let’s look at an example;
print('baransel.dev Python Lessons')
print("baransel.dev Python Lessons")
print("""baransel.dev Python Lessons""")
Output:
baransel.dev Python Lessons
baransel.dev Python Lessons
baransel.dev Python Lessons
and you will get the same result in all three uses, why are there three different ways of use?
Let me explain it to you with a few examples;
You want to get an output like this;
Baransel’s class ended early today.
If you write the example like this;
print('Baransel's class ended early today.')
Output:
File "/Users/baran/Downloads/test.py", line 1
print('Baransel's class ended early today.')
^
SyntaxError: invalid syntax
In other words, the reason for the syntax error is that Python starts with the first quotation mark and ends when it sees the second quotation. Since it cannot read the text after the second quotation, it gives a syntax error. For this, a usage like this would be more accurate.
print("Baransel's class ended early today.")
print("""Baransel's class ended early today.""")
Parameters of the print()
Function:
If anyone has asked what a parameter is, don’t worry. I will explain the parameters about the function in detail, don’t worry about it for now, what you need to know for now is how it is used. Let’s start with the first parameter;
end
Parameter
With this parameter in the print()
function, we specify what operation we will do at the end of the values we give to the screen, let’s show its use with an example;
Let’s write two print()
functions in a row;
print("baransel.dev Python Lessons")
print("baransel.dev Python Lessons")
Output:
baransel.dev Python Lessons
baransel.dev Python Lessons
So how can we print these two print()
functions side by side;
print("baransel.dev Python Lessons",end="")
print("baransel.dev Python Lessons")
Output:
baransel.dev Python Lessonsbaransel.dev Python Lessons
sep
Parameter
With the previous parameter, we showed what operation we will do at the end of the values, but how we will do different operations between these values, we will do it with the sep parameter, let’s show it with a couple of examples;
print("baransel", "dev")
Output:
baransel dev
As you can see, Python concatenates the values with a space, but we didn’t specify it because if we don’t specify anything, Python takes it as a space by default. How do we concatenate them with a dot between them? Its use is the same as the sep parameter;
print("baransel", "dev", sep=".")
Output:
baransel.dev
*(star)
Parameter
In this parameter, it allows us to operate between the data like the sep
parameter or use characters, but in the sep
parameter, it allows us to operate on the same variable if it provides between different variables and elements.
print(*"Baransel")
Output:
B a r a n s e l
You will get an output like this parameter is used together with the sep parameter. Let’s show it with an example;
print(*"Baransel", sep=".")
Output:
B.a.r.a.n.s.e.l
In the print()
function; There are file
and flush
parameters, since these parameters are the parameters that allow us to process the files, I will not tell them for now.