Print Function

An introduction to Python’s print() function, explaining its syntax, parameters, and usage with practical examples for outputting text and other data to the console or files.

Loading...
Print Function

What is print()?

The print() function is used to display something on the screen.

For example:

print("Hello World!")
 
# Output: Hello World!

Python print function

You can use print() to display text, numbers, or other values.

print("Hello") 
print(42)
 
# Output:
Hello 
42

Python 2 vs Python 3

If you come across older Python code, you might see:

print "Hello"

This was valid in Python 2.

In Python 3, print is a function, so we use parentheses:

print("Hello")

Since we’re learning Python 3, you’ll always use print().

Using multiple values

You can pass multiple values to print():

print("Hello", "World")
 
# Output:
Hello World

By default, Python adds a space between the values.

You can change this using the sep parameter.

sep

sep tells Python what to put between multiple values.

print("Hello", "World", sep=", ")
 
# Output: 
Hello, World

The default value of sep is a space.

end

By default, print() moves to a new line after displaying something.

print("Hello") 
print("World")
 
# Output:
Hello
World

You can change this using the end parameter:

print("Hello", end=" ") 
print("World")
 
# Output: 
Hello World

Here, instead of moving to a new line after Hello, Python adds a space.

One more thing to know

print() shows something on the screen, but it doesn’t give that value back.

For example:

x = print("hi")

This will print hi, but x will not contain "hi".

x will contain None, which basically means “nothing.”

Python print function


👉 Next tutorial: Python Escape Sequence Characters