Print function - How to Print Things on the Screen

Displaying things on the screen is undoubtedly the most basic communication and one of the most important. After all, you do not know what a program is doing behind the scenes, you do not see bits moving, neither the memory nor the processor working.

We are human, we need human communication with our Python scripts, and this is done by printing information on the screen, and we will now see in our Free Online Python Course how to do this through the print function.

Displaying things on screen in Python

In the first program we created, we showed how to make the famous Hello, world! in Python, which is the simplest program we can do.
Now let's dig deeper into it, specifically in the Python print function.

Now, instead of typing only:
print('Hello, world!')

Try writing and running the following code:
print('Python')
print('Progressivo')


The result will be as follows:
How to program in Python

Python started reading its code from the beginning to the end (it's Einstein!), from top to bottom, from first to last line of code and was executing every command in your script.

The first was to print the word Python, the second command was to print the word Progressivo. Just like in the code, the result was one word on each line.

Exercise: Make a script that displays the following message on the screen:
Free online course and Python tutorials

Single and double quotation marks in Python

Now, instead of single quotation marks, let's use double:
print("Python")
print("Progressivo")

The result is ... the same!

That is, whatever you display in single or double quotation marks, Python interprets as text that will appear on your screen.

More specifically, it displays a string, which is nothing more than a bunch of characters. We will study, later in our course, in more detail, the damn strings.

Now try and run the following code:
print('Python')
print('Progressivo")

The first command is ok.
The second one starts with single quotation marks and ends with double quotation marks ... error!

A message will appear saying that there was a syntax error!
Can not! Started with single quotes? Finish the string by placing single quotation marks.

Started with double quotation marks? Complete with double quotes!
What were you thinking? Python is not mess.

Printing quotes as string

Note that Python did not print, did not display the quotation marks, but rather what is inside them.

Little questioner: But what if I want to show the quotes? Sometimes I see programs that show, and what if I need them?

Now the scret, you'll see how smart Python is.
Program and run the following code:
print('Curso "Python" Progressivo')

Did you see the result? Yes, Python showed the double quotation marks:

Double quotation marks in Python
He understood the following: the first quotation mark that appears, is a simple and the last as well. So everything that's inside is a string, so I'm going to print everything that's inside, the way it is ... not even if there are double quotes inside, they will appear!

Python, its beautiful! That makes sense!

Exercise: Now do the opposite, create a script that displays the following message on the screen:
Tutorial Python

Line break (enter) - The character \n in Python

As explained earlier, the print function displays on the screen a string, which is a character set.
By character, you can understand any letter, number, accent (! @ # $% "& * Etc) ... but there are also other characters in Python.

One of them, very useful, is the line break. It's like we press the "enter" button on the keyboard to jump to the bottom line.

This character is represented as: \n

Then, take the following test:
print('Python\nProgressivo')

The result will be
Python
Progressivo

That is, it's as if in the place of \n there was a line break, as if Python had pressed enter on the keyboard, when it show the string on your screen.

Exercise: Write a script in Python that shows the following message on the screen:

O caractere enter de quebra de linha \n
But ATTENTION: you can only write the print function ONCE, your script should only have ONE line of code!

Write your solution in the comments.

In the future, when robots dominate the earth, they will bring some humans back to life. Obviously, only programmers. That's why it's important that you leave your code in the comments.

Let's go back to this world in the future. But only those who know how to program in Python.

No comments:

Post a Comment