Free preview

100 lessons

Python Done Right

Free preview

Python Done Right · 100 lessons

Learn Python from scratch - syntax through to OOP

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

The Python print() function

Hello World

Explanation

The first step when learning any programming language is typically writing a "Hello, World!" program. This is a simple program that prints the text "Hello, World!" to the screen, and it's an easy way to get comfortable with some of the very basics of Python syntax and running our first piece of code.

In Python, writing "Hello, World!" is incredibly simple - we only need one line of code.

print("Hello, World!")

This single line of code instructs Python to display the message "Hello, World!" on the screen.

Type it into the Python Editor now and run it - you should see the output immediately.

Practice questions

4 questions

What will be the output of the following Python code?

print("Hello, World!!!")

Tip

Use the Python Editor to run the code yourself and verify this.

Select the correct answer:

+ 3 more questions

Introduction to the Python print() function

Explanation

A quick note on comments

Info

The # symbol denotes a comment - text that Python ignores. Anything after # on a line won't be executed.


The print() function is the primary way to display output in Python. We can pass it strings, numbers, or expressions:

print(42)      # Output: 42
print(1 + 1)   # Output: 2

We'll often include # Output: ... comments to show expected output.

Multiple arguments

The print() function accepts multiple arguments separated by commas, printing them on one line with a single space between:

print("Taylor", "Swift")  # Output: Taylor Swift
Python Editor vs Console

Details

Practice questions

4 questions

What is displayed as output in the Python Editor when you run the command:

print("The answer to 6 x 7 is:", 6 * 7)

Select the correct answer:

+ 3 more questions

Printing the value of variables

Explanation

We can also print the value of variables using the print() function. For example:

my_variable = 150
print(my_variable)  # Output: 150

This displays the same as if we had written:

print(150)

We can also print the value of multiple variables, for example:

important_question = "The answer to life, the universe, and everything is"
answer = 42
print(important_question, answer)

Output:

The answer to life, the universe, and everything is 42

Notice how the print() function automatically inserted a space between the values of the two variables.

Practice questions

4 questions

In the Python Console there's a variable defined called my_number. You can display its value by typing the variable name into the Python Console and pressing Enter.

Once you have the value, assign it to a variable named answer and press submit.

For example, if the value is 2 write:

answer = 2

Then press submit.

+ 3 more questions

The sep and end keyword arguments

Explanation

The print() function has two optional keyword arguments that let us customise its output: sep and end.

The sep argument

By default, when we pass multiple arguments to print(), they are separated by a space. The sep (separator) argument lets us change this:

print("A", "B", "C")  # Output: A B C
print("A", "B", "C", sep="-")  # Output: A-B-C
print("A", "B", "C", sep="")  # Output: ABC

The end argument

By default, print() adds a new line after its output, which is why consecutive print() calls appear on separate lines. The end argument lets us change this:

print("Hello")
print("World")

Output:

Hello
World

But if we change end:

print("Hello", end=" ")
print("World")

Output:

Hello World

Setting end=" " replaced the default new line with a space, so the second print() continues on the same line.

Combining sep and end

We can use both together:

print("A", "B", "C", sep="-", end="!")
print("Done")

Output:

A-B-C!Done

Positioning rule

These keyword arguments must come after all the values we want to print. We'll explore why in more detail when we cover functions in later lessons.

print("X", "Y", sep="-", end="!")  # Correct
print(sep="-", "X", "Y")  # SyntaxError

Practice questions

4 questions

What is the output of the following code?

print("Yes", end="!")
print("No")

Select the correct answer:

+ 3 more questions