Before we continue, I need to tell you about the Python
What is a function?
We haven’t talked about functions yet, but we will cover functions in-depth soon. For now, it suffices to know that a function is something you can call. It will do something specific for us, and the name often implies what it does. Functions often accept arguments, which they can use to do their specific job.
The Python print function
print() function can print text to our screen. We can feed one or more arguments, and these arguments will be printed on the screen. Let’s try some print calls. The following code example is interactive, meaning you can edit it and run it. Read the code, press the run button, and inspect the result:
A couple of observations that you might have caught yourself:
- Python’s print is called by entering its name followed by parentheses.
- The optional arguments are listed between the parentheses, separated by commas.
- You can print both numbers and strings. In fact, most data types in Python can be printed.
- You can mix argument types, like numbers and strings.
- In its most basic form, we can call print without any argument. In this case, it prints an empty new line.
print()function will convert special characters into something meaningful. E.g., the newline character
\nturns into an actual newline, and the tab character
\twill get converted into an actual tab.
- print() won’t escape strings like the REPL does, because we don’t want to show escaped strings to the users of our software. It’s just a convenience thing the REPL offers us, the programmers.
You’re learning about print this early because you’ll encounter it in almost every Python program and example on the web. Another reason is that the interactive code examples can only show data by using print, like the one above. Now that you know print, I can include a lot more of these interactive examples which are a great learning aid!