Python Print Function

print() is used to display text or output values in Python. In this lesson, we'll learn how to use the print() function effectively and format the output.

Printing Strings

You can use single, double, or triple quotes to print strings (text).

But if your quotes don't match correctly, or you leave them out entirely, Python will throw an error:

Printing Numbers

To print numeric values, you don't need any quotes:

Printing on the Same Line

So far, every time we've used the print() function, the next print() started on a new line.

For example:

Output

Hello World!
How are you?

Notice that "How are you?" is printed on the next line. But what if you want both outputs to appear on the same line instead?

Output

Hello World! How are you?

The extra part inside print(), end=" ", is called a parameter. It tells Python what should be printed after the output.

Normally, Python automatically moves to the next line after every print(). It does this by using a special character called the newline character, written as \n.

So when you write:

Python behaves as if you had written:

The \n tells Python to move the cursor to the beginning of the next line. When you change end="\n" to end=" ", you're telling Python:

"Don't move to the next line. Print a space instead."

That's why the next print() continues on the same line.

Using No Space

If you don't even want a space between the outputs, use an empty string.

Using Custom Text

The end parameter can contain any text you want.

You can use the end parameter whenever you want to control what appears after the output instead of moving to a new line.

Printing Multiple Lines with One print()

The newline character (\n) can also be written inside a string. Whenever Python sees \n inside a string, it starts printing from the next line.

Printing Text with Numbers

Printing Variables with Text