3.4. The print Function

In the Hello World section, you practiced displaying text on the screen. Technically, you sent the words to the console, which is a simple window where the user can type commands or view output. We used the print function without really talking about how it works. Let’s fix that now.

We call the function using the syntax print(). When the code runs, we want it to tell the computer, Please display what is inside the () on the screen. For us, the word print is simple enough. However, the computer only understands binary instructions.

When we run the program, the compiler works behind the scenes to convert print() into the 0s and 1s that tell the computer: Show this information in the console.

3.4.1. Examples

Run the example below, and note the difference between the outputs:

Observations line by line:

  1. In the line 2, we print some text, which is surrounded by quotes.
  2. In the line 3, we print a number. Note the absence of quote marks.
  3. In line 4, we use four words separated by commas, all within the same set of parentheses (). When the words print, they show up on the same line separated by spaces.
  4. The code in line 5 puts extra spaces after the commas. How does this affect the output?
  5. Line 6 prints two words, but in this case the code uses + instead of a comma. This prints the words without spaces in between.
  6. Line 7 prints text and a number with a space in between.

3.4.2. Two Special Characters

For all of the examples above, each time we use print a newline is added after the information. Think of a newline as hitting the Enter or Return key on your keyboard. The cursor moves to the beginning of the next line.

For the computer, newline as an invisible character that tells the machine to move to the next line. We can use this invisible character by adding the combination \n.

Try It

Experiment with the newline character by adding these lines to the editor above:

8
9
print("Some Programming Languages:")
print("Python\nJavaScript\nJava\nC#\nSwift")

In addition to the newline character, there is also a special tab character, \t. Go back to the examples above and play around with using \t and \n.