Print

print

Print

Video Transcript

In this section, we're going to learn how to print to the console. Printing to the console allows us to see the results of our program, and can help us find bugs if we make any mistakes. To print to the console, we can use Python's built-in function print.
print is an example of a function in Python. Functions have a unique name, and to use them, we simply write their name followed by a pair of parentheses. Often, functions need extra information called arguments in order to work, which are included inside the parentheses:
print("Hello World!")
# Hello World!
When we run our program, we can see the string in the parentheses shows up in the console!
We can print almost anything in Python, including numbers, variables, and more:
print(45)
# 45
  
greeting = "hello"
print(greeting)
# hello
Python strings also have certain special characters. The most important ones are:
  • \n (creates an extra line)
  • \t (adds a tab)
If we take the line from before, print("Hello World!"), place a \t before "Hello", and replace the space with a \n, "Hello" will now be indented, and the output will appear on two lines:
print("\tHello\nWorld!")
#   Hello
# World!
We can use these special characters and more to make our strings look exactly the way we want when printing to the console.
Last section Next section