← All coding practice

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

Python Coding Exercise #05: Extra Space

Part of the series Python Practice Exercise — part 5

Before you start: Know print() and variables · Know what an f-string is

The question

Your friend is working on an app for jobseekers. She sends you this bit of code:

name = "Tim Tester"
age = 20
skill1 = "python"
level1 = "beginner"
skill2 = "java"
level2 = "veteran"
skill3 = "programming"
level3 = "semiprofessional"
lower = 2000
upper = 3000

print("my name is ", name, " , I am ", age, "years old")
print("my skills are")
print("- ", skill1, " (", level1, ")")
print("- ", skill2, " (", level2, ")")
print("- ", skill3, " (", level3, " )")
print("I am looking for a job with a salary of", lower, "-", upper, "euros per month")

The program should print out exactly the following:

my name is Tim Tester, I am 20 years old

my skills are
- python (beginner)
- java (veteran)
- programming (semiprofessional)

I am looking for a job with a salary of 2000-3000 euros per month

The code works almost correctly, but not quite. The exercise has very strict tests, which check the output for every single bit of whitespace. Fix the code so that the printout looks right. Notice especially how the comma notation in the print command automatically inserts a space around the different comma-separated parts.

What the friend’s code prints

Here is the output of the code as it is:

my name is  Tim Tester  , I am  20 years old
my skills are
-  python  ( beginner )
-  java  ( veteran )
-  programming  ( semiprofessional  )
I am looking for a job with a salary of 2000 - 3000 euros per month

At a glance it looks close, but compare it with the required output and you’ll find three kinds of problems:

  1. Extra spaces around the names, skills, levels and numbers, for example my name is Tim Tester , I am.
  2. Missing blank lines after the first line and after the skills list.
  3. Spaces in the wrong places, such as ( beginner ) instead of (beginner), and 2000 - 3000 instead of 2000-3000.

Why the extra spaces appear

When you give print() several values separated by commas, it prints them one after another and puts one space between each pair. That space is added automatically, on top of any spaces that are already inside your strings.

print("a", "b")    # a b

Now look at the first line of the friend’s code:

print("my name is ", name, " , I am ", age, "years old")

The string "my name is " already ends with a space, and print() adds another one before name. That’s two spaces. The same happens around " , I am ", and that’s where Tim Tester , I am comes from. Every comma in the friend’s code adds a space, and the strings add more.

My Solution

The easiest fix is to stop using commas and build each line as one f-string (introduced in Exercise #04), so you control every character yourself:

name = "Tim Tester"
age = 20
skill1 = "python"
level1 = "beginner"
skill2 = "java"
level2 = "veteran"
skill3 = "programming"
level3 = "semiprofessional"
lower = 2000
upper = 3000

print(f"my name is {name}, I am {age} years old\n")
print("my skills are")
print(f"- {skill1} ({level1})")
print(f"- {skill2} ({level2})")
print(f"- {skill3} ({level3})\n")
print(f"I am looking for a job with a salary of {lower}-{upper} euros per month")

Output:

my name is Tim Tester, I am 20 years old

my skills are
- python (beginner)
- java (veteran)
- programming (semiprofessional)

I am looking for a job with a salary of 2000-3000 euros per month

How My Solution Works

1. f-string adds exact spacing

In an f-string, a space is printed only if you type one. So f"my name is {name}, I am {age} years old" has exactly one space between each word and none before the comma.

2. The numbers don’t need converting

age, lower and upper are numbers (int), not strings. An f-string handles that for you. It turns each value into text as it inserts it, so you don’t need str().

3. \n adds the blank lines

print() always ends its output with a newline, which is why each print() starts on a fresh line. Adding \n inside the string adds one more newline, and that shows up as a blank line:

  • ...years old\n produces a blank line after the first line.
  • ...({level3})\n produces a blank line after the last skill.

If you prefer, an empty print() does the same job, because it prints just a newline:

print("a")
print()
print("b")
a

b

So you could replace the \n at the end of a string with a separate print() on the next line. Both give identical output. (I cover \n in the strings section of Data Types in Python.)

Another way to fix it: sep=""

If you want to keep the comma style, print() has an option called sep (short for “separator”) that sets what goes between the values. Its default is a single space. Setting it to an empty string removes the automatic spaces:

print("a", "b")             # a b
print("a", "b", sep="")     # ab
print("a", "b", sep="-")    # a-b

The whole program can be fixed this way too, as long as you put every space you want inside the strings yourself:

print("my name is ", name, ", I am ", age, " years old\n", sep="")

This gives the same output, but the f-string version is easier to read, because you can see the finished sentence in one piece.

How to spot invisible whitespace

The hardest part of this exercise is that you can’t see a trailing space or a doubled space. A few habits help:

  • Compare character by character with the sample output, especially around commas, brackets and dashes.
  • Use print(repr(text)) on a string to see its exact contents. Spaces and \n become visible.
  • Remember that exercises with strict tests treat a stray space exactly like a wrong letter.

As the exercise itself warns, be extra careful about formatting printouts in the future. Some tests require the output to match the example exactly.

Key takeaways

  • Commas in print() put an automatic space between values, in addition to any spaces inside your strings.
  • An f-string builds the whole line yourself, so only the spaces you type appear.
  • \n inside a string adds a newline, so ...\n at the end of a printed string leaves a blank line. An empty print() does the same.
  • print() has a sep option that changes the automatic separator. sep="" removes it.
  • When the output must match exactly, whitespace is part of the answer. Check it 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 · f-strings · strings · whitespace