The built-in print() function

Let’s start building the project using built-in functions. We have already seen some built-in functions, and we know that a function does the following things:

  1. It takes something as its input.

  2. It processes this input to create some output.

  3. It returns the output.

The most prominent Python built-in function that prints something on the screen is print. It prints anything we give as input on the screen. For instance, guess the result of the following code when you press the “Run” button?

print(3.1415)

It simply prints the exact number you gave as input to the print function. This function gives us the power, as a programmer, to verify what the computer has done for us. For example, we know what the built-in function round is supposed to do. It takes any number with a decimal point and rounds it to the nearest whole number, meaning 3.1415 should get rounded to 3. Try it yourself by pressing the “Run” button:

round(3.1415)

There wasn’t any error, and the computer seems to have done its job, but we couldn’t really confirm if the round function worked as we expected. So, what happens if we use print and round together? We know by now that a method can take an input, do something with it, and return a processed output. The method named round() can take as input 3.1415, process it to truncate the number to the nearest whole number, and then return the whole number 3. Is there a way for us to take all of what round as a method does and give that as input to the print method? Study the code below carefully, and then run it to see the output:

print(round(3.1415))

To understand the flow of how the above statement works, let’s look at the illustration below.

canvasAnimation-image
1 of 5

The project requires printing on the screen

Let’s attend to our client’s requirements for the project, armed with our new Python knowledge. There are at least three major project requirements (2, 3, and 9), from the following, that will use our knowledge of the print() function.

The project requirements that require printing on the screen
The project requirements that require printing on the screen

Printing the score

We’ll begin by attempting to print the user’s score on the screen (requirement 9). Let’s imagine the student has managed to score 4 marks. Can you print 4 on the screen so they can see it?

We can even make the computer work for us and compute the correct answer to a sum-based question. Let’s say the student was asked to solve 2 + 8. Regardless of the answer, our computer should know the correct answer to this question (requirement 6).

print(2 + 8)

This code does not print 2 + 8 on the screen, it prints 10. This means that the computer knows how to resolve the + operator, which is great.

Printing a text message

But sometimes, we don’t want the computer to compute or resolve anything, we simply want it to print whatever we give it to print. For example, for requirement 9, we need to print a message on the screen for the student because the computer needs to engage with users. Simply printing out a 4 on the screen at the end will not be the best practice. We need to be able to tell the user that this 4 was their actual score. Since we have already used the print() function to print a number, we will use the same function to print a text message as well. Click the “Run” button in the code widget below:

print(You have managed to score)

Well, that did not work as per our expectationsDon’t worry if you don’t understand the exact meaning of the error message at the moment.. Can you think of any reason why print(4) worked like a charm but not print(You have managed to score)? Perhaps we have confused Python’s print function with this text input.

Remember what we normally do in our novels when we want to separate the dialogue from the normal text? We put quotation marks around the dialogues within the text. We have to do the same here as well. The Python code we are writing is also text, e.g., print is a Python command but is written as text, and the input to the print function is also text, You have managed to score. We need to put double quotes around the text message before printing:

print("You have managed to score")

Python does not like it if you miss one quotation or a parenthesis. They come in pairs!

While we are only trying to get to know how Python and programming work, let’s try one little experiment. Don’t be afraid of experimenting with your own ideas. What’s the worst that can happen? At most, your code won’t work, and Python will try to complain about it. Right now, we only care deeply about conceptual clarity.

Try running the following four Python programs one by one. But before clicking the “Run” button, try imagining what the output for each program should be:

print("Your score is 10")
print("10")
print("2 + 8")
print(2 + 8)

Python prints anything inside quotation marks verbatim (on an as-is where-is basis). That’s why 2 + 8 with quotation marks is printed as is, while 2 + 8 without quotation marks prints 10.

Concatenating the text

We know what the + operator does for numbers. We have seen that Python also knows the meaning of +. But here is where the similarity ends. Can you think of another way the same + can be used for two text messages? For example, what would be the result of:

"You have managed" + "to score 10 marks"

Of course, adding or summing up only makes sense for numbers.

Try it in Python yourself in the widget below. Print the above to see what output we get:

So Python also lets us use + for two pieces of text as well—it joins the two pieces of text together. But if you look closely, it ate up the space between “managed” and “to,” and the student will be confused to read the word “managedto.” Sometimes, we need to rescue Python from the mistakes it makes. Here, to help Python, why don’t we try putting an extra space before closing the quotation marks around managed?

Take away:

The meaning of + is slightly different for text. Python concatenates or joins the two pieces of text with +.

Another lesson here is that there seem to be multiple ways of achieving results in Python, just as it is in life.

Printing the text message and score together

In one arrangement, we can see that Python even lets us use a comma-separated print function:

print("You have managed" , "to score 10 marks")

We know some functions can take two inputs, such as a power functionpow(10,2) means 10 raised to the power 2, which is 100. However, this power function requires two input arguments, not one.. The same is the case with print. Python lets us give as many inputs (separated by commas) to the print function as we require. This comes in handy because we can ask it to print two things for us in a row, or however many we want. We can also ask it to print a text message as well as a number, separated by commas:

print("First message" , "Second message", 1, 2, 3, "Third message")

This gives us the power to fulfill another project requirement. Let’s imagine that we want to tell the learner about what the correct answer to a question is, but we want the computer to work it out for us because we know it’s flawless in computing sums:

print("The correct answer for 8 + 2 is:", 8 + 2)
print("The correct answer for 816234 + 256423 is:", 816234 + 256423)

Nice! So, Python lets us write more than one line of code, and executes it one by one!

Test yourself

Can you spot the error in this code?

Q
print("Here is some text)"
A)

The closing parenthesis for the print() function is absent.

B)

We have used the wrong symbol to enclose text.

C)

There is no error in this code.

D)

There must be a number as an input to the print() function.

The next project requirement

We’re doing pretty good so far. We have learned what it takes to output something on the screen. With the help of the print function, we have grown confident enough to understand our client’s requirements for the Python project. But let’s not jump ahead too quickly. In the next lesson, we’ll navigate the demands of requirements 1 and 5—that we take something as input from our app’s users.