Skip to Content
Nextra 4.0 is released 🎉
Input and OutputOutputPrinting Output

Printing Output

In Python, you can print output to the console using the print() function.

It is one of the most commonly used functions in Python and something you will use frequently during this class.

Basic printing

To print a simple message, you can use the print() function like this:

# Example of basic printing print("Hello, World!")

The string we want to print needs to be placed inside the parentheses and enclosed in quotes.

When you run this code, it will output:

Hello, World!

We are not limited to printing just strings though. We can also print numbers and other data types. For example:

# Printing different data types print(42) # Printing an integer print(3.14) # Printing a float print(True) # Printing a boolean

This will output:

42 3.14 True

Printing variables

To print variables in Python, you simply pass the variable name to the print() function. For example:

# Printing variables name = "Alice" age = 30 print(name) print(age)

This will output:

Alice 30

You’ll see in the next sections that you can combine strings and variables together in your print statements for more complex output.

Printing multiple items

You can also print multiple items at once by separating them with commas inside the print() function. For example:

# Printing multiple items name = "Alice" age = 30 print("Name:", name, "Age:", age)

Here we are printing a string followed by the value of the variable name, then another string followed by the value of the variable age.

This will output:

Name: Alice Age: 30

When printing multiple items, Python automatically adds a space between each item. You can think of the comma turning into a space in the output.

These are basics of printing in Python. In the next section, you’ll learn about more advanced printing techniques that will help you format your output better.

Last updated on