← All coding practice

Coding Practice · Beginner · October 25, 2026 · 3 min read

Python Coding Exercise #59: Underlining

Part of the series Python Practice Exercise — part 59

Before you start: Know while True and break · Know len() · Know string multiplication

The question

Please write a program which asks the user for strings using a loop. The program prints out each string underlined as shown in the examples below. The execution ends when the user inputs an empty string - that is, just presses Enter at the prompt.

Please type in a string: Hi there!
Hi there!
---------
Please type in a string: This is a test
This is a test
--------------
Please type in a string: a
a
-
Please type in a string:

Before coding: putting three earlier ideas together

This exercise doesn’t need anything new. It combines three things from the last few posts:

IdeaUsed for
A while True loop with break (Exercise #43)Asking again and again, and stopping on an empty string
len() (Exercise #30)Counting the characters of the string
String multiplication (Exercise #54)Making a line of dashes as long as the string

The underline has to be exactly as long as the string above it. A string of 9 characters needs 9 dashes, so the number of dashes is len() of the string, and "-" * len(word) makes them.

The stopping rule is that the user presses Enter without typing anything. In that case, input() returns an empty string, "", which has no characters at all.

My Solution

while True:
    word = input("Please type in a string: ")
    if word == "":
        break
    print(word)
    print("-" * len(word))

How My Solution Works

1. The loop asks every round

while True: repeats its block until break runs, and word = input("Please type in a string: ") asks for a new string at the start of each round.

2. An empty string ends the program

if word == "": is true only when the user pressed Enter without typing anything. Then break ends the loop at once. This check comes before the two print() lines, so nothing is printed for the empty string.

3. Otherwise, print the string and its underline

  • print(word) shows the string.
  • print("-" * len(word)) shows a line of dashes. len(word) is the number of characters, and "-" * len(word) repeats the dash that many times.

Then the loop goes back to the top and asks again.

Following the sample

RoundThe user typesEmpty?Python prints
1Hi there!NoHi there!, then 9 dashes
2This is a testNoThis is a test, then 14 dashes
3aNoa, then 1 dash
4(just Enter)Yesbreak, the program ends

The space between Hi and there! counts as a character, which is why Hi there! gets 9 dashes and not 8.

University Solution

while True:
    string = input("Please type in a string: ")
    if string == "":
        break
    print(string)
    print(len(string) * "-")

The University Solution is the same program as My Solution, with two small differences:

  • A different variable name: string instead of word.
  • The multiplication is written the other way round: len(string) * "-" instead of "-" * len(word). A string multiplied by a number and a number multiplied by a string give the same result.

Both programs print identical output for the sample and for every other input tested.

Things to notice

Spaces get an underline, too. A string made only of spaces is not an empty string, because it has characters. If the user types three spaces, the program prints the three spaces and then ---. Only pressing Enter with nothing typed ends the program.

The check comes first. If the if word == "": check were written after the two print() lines, the empty string would be printed with an empty underline before the program stopped.

Nothing prints for an empty first string. If the user presses Enter on the very first prompt, the loop ends in the first round, and the program prints nothing at all.

Key takeaways

  • input() returns an empty string, "", when the user presses Enter without typing anything.
  • Comparing with "" is a way to test for “nothing typed”, and it works well as the stop signal of a loop.
  • Check the stop signal before using the input.
  • "-" * len(word) makes a line of dashes as long as the word.
  • A string of spaces is not empty.

✦ 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 · loops · while-loop · break · strings · string-multiplication