← All coding practice

Coding Practice · Beginner · October 4, 2026 · 4 min read

Python Coding Exercise #07: Print a Single Line

Part of the series Python Practice Exercise — part 7

Before you start: Know print() · Know what a string is

The question

Each print command usually prints out a line of its own, complete with a change of line at the end. However, if the print command is given an additional argument end = "", it will not print a line change.

For example:

print("Hi ", end="")
print("there!")
Hi there!

Please fix this program so that the entire calculation, complete with result, is printed out on a single line. Do not change the number of print commands used.

print(5)
print(" + ")
print(8)
print(" - ")
print(4)
print(" = ")
print(5 + 8 - 4)

What the program prints now

Run as it is, the program prints every piece on its own line:

5
 + 
8
 - 
4
 = 
9

The goal is the whole calculation on one line: 5 + 8 - 4 = 9.

My Solution

print(5, end="")
print(" + ", end="")
print(8, end="")
print(" - ", end="")
print(4, end="")
print(" = ", end="")
print(5 + 8 - 4)

Output:

5 + 8 - 4 = 9

How My Solution Works

1. Every print() ends with a newline

After printing its values, print() automatically adds a newline, the invisible character that moves the cursor to the next line. That is why each print() normally gets a line of its own.

The newline comes from a setting called end, which defaults to "\n". You can change it by passing your own value:

print("a", end="-")
print("b", end="-")
print("c")
a-b-c

Here the first two prints end with - instead of a newline, so the next output continues on the same line.

2. end="" means “end with nothing”

If you set end to an empty string, print() adds nothing at the end, so the next print() carries on straight after it. Adding end="" to the first six lines glues all the pieces together on one line.

3. The last line keeps the default

Look at the final line of the solution:

print(5 + 8 - 4)

It has no end="", on purpose. This last print() prints the result 9 and then finishes the line normally. If it also ended with nothing, anything printed afterwards would stick onto the end of this line:

print(5, end="")
print(" + ", end="")
print(8)          # this one ends the line normally
print("after")    # this starts on a new line
5 + 8
after

4. The spaces are already there

The strings " + ", " - " and " = " each have a space on both sides, so the spaces in 5 + 8 - 4 = 9 come from the strings. The numbers 5, 8 and 4 need no quotation marks, because print() turns a number into text on its own.

Because spaces inside a string are real characters, they must be exactly right. A stray space would show up in the output, just like in Exercise #05.

sep and end: two different settings

print() has two options that are easy to mix up:

OptionControlsDefault
sepwhat goes between the values in one print()a space
endwhat goes after the last valuea newline

You can use both together:

print("a", "b", sep="-", end="!")
print("c")
a-b!c

The sep put a - between a and b, and the end put a ! after b instead of a newline, so c continued on the same line.

How you would normally write this

This exercise says not to change the number of print commands, so seven print() calls with end="" is the right answer here. In everyday code, you would more likely build the whole line in a single f-string (see Exercise #04):

print(f"5 + 8 - 4 = {5 + 8 - 4}")
5 + 8 - 4 = 9

Both give the same output. The end="" trick is most useful when you print inside a loop or build a line gradually, and you want everything to stay on one line.

Key takeaways

  • By default, print() adds a newline after its output. That is why each print() appears on its own line.
  • The end argument changes what print() adds at the end. end="" adds nothing, so the next output continues on the same line.
  • Keep the default on the last print() so the line finishes normally.
  • sep controls what goes between values. end controls what goes after the last one.
  • Numbers don’t need quotation marks in print(). Python converts them to text for you.
  • Spaces inside strings are real characters, so check them carefully.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · print · output · strings