The Basics of Printing in Python

This article is a complementary resource to the Learn Python Basics course.

The Basics of Printing in Python

As you know, we use the print() function to display output on the screen. For example,

name = "John"
age = 10

print("Name:", name)
print("Age:", age)

A few things to notice about print()

The print() function in Python automatically adds a space between variables. Any extra spaces you include in the code are disregarded. For example:

age = 10
print("Age:", age)

#Output: Age: 10

You might think that the space between "Age:" and age is caused by the space after the comma.

However, that's not the case—the space is automatically added by print(), regardless of how many spaces you have after the comma.

age = 10

print("Age:", age)
print("Age:" , age)
print("Age:"     ,     age)

Here, all the print line provides same output:

Age: 10
Age: 10
Age: 10

What if you don't want the space and want the output to be like this?

Age:10

You can use an f-string to gain more control over formatting:

age = 10
print(f"Age:{age}")

#Output: Age:10

This will print exactly as you want, without any spaces between "Age:" and the value of variable.


Python sep

Previously, we discussed that print() automatically adds a space between variables, even if there is no space explicitly written. This happens because Python uses a space as the default separator.

age = 10
print("Age:", age)

#Output: Age: 10

is essentially the same as:

age = 10
print("Age:", age, sep=" ")

#Output: Age: 10

The sep parameter specifies the string that will be used to separate multiple values when printed, replacing the default space between them.

Now, if you don't want space after "Age:" and age. You can simply use the following code:

age = 10
print("Age:", age, sep="")

# Output: Age:10

We can also add a custom separator as:

age = 10
print("Age:", age, sep="<-->")

# Output: Age:<-->10

Python end

Whenever you use print() in Python, the next statement automatically starts on a new line. This is because, by default, print() adds a newline character (\n) at the end of the output.

age = 10
name = "John"
print("Age:", age)
print("Name:", name)

Output

Age: 10
Name: John

is same as

age = 10
name = "John"
print("Age:", age, end="\n")
print("Name:", name, end="\n")

Output

Age: 10
Name: John

Here, end="\n" adds a new line after each print() statement. You can customize the end parameter to change this behavior.

For instance, you can use an empty string (end="") if you don't want a newline after the output:

age = 10
name = "John"
print("Age:", age, end=" ")
print("Name:", name)

Output

Age: 10 Name: John

You can also use other string:

name = "John"
age = 10

print("Age:", age, end="------")
print("Name:", name)

#Output: Age: 10------Name: John

Takeaway:

  • Use f-strings for printing multiple variables, like print(f"Age: {age}"), as they offer better readability than using sep.
  • To avoid a newline after print(), use the end parameter: print("Age:", age, end="").