Exercise: Hyphenated Words

Exercise 13 Very easy

Prerequisites for the exercise

  1. Python String Methods
  2. Python String Basics
  3. All previous chapters

Objective

Extract out individual words from a hyphenated word.

Description

Ask the user to input a hyphenated word by the following input prompt message:

Enter a hyphenated word:

Once the word is given, extract out the constituent words from it, and print each on a new line.

Shown below is an example:

Enter a hyphenated word: awe-inspiring

awe
inspiring

If the input word is not hyphenated i.e. there is not a hyphen (-) in it, print 'Invalid input!', and keep asking the user to enter a hyphenated string, as shown above, until the correct input is received.

Shown below is a demonstration:

Enter a hyphenated word: amazing
Invalid input!

Enter a hyphenated word: programming
Invalid input!

Enter a hyphenated word: strongly-typed

strongly
typed

Note the blank lines over here.

If the input value is invalid, print 'Invalid input!' and then leave a blank line before asking the user to input again. If the input value is valid, leave a blank line before printing out all the constituent words in the hyphenated word.

Hints

Hint 1

Use the split() string method.

View Solution

New file

Inside the directory you created for this course on Python, create a new folder called Exercise-13-Hyphenated-Words and put the .py solution files for this exercise within it.

Solution

The description clearly says to keep on asking the user to enter a hyphenated word unless the value is valid.

This reminds us of a loop, whose condition is True and which is terminated (using break) only if the entered word is hyphenated.

Obviously, the loop's condition could directly be set to check the input value, but using True is easier.

Anyways, now that we know that a loop is needed, let's set it up:

while True:
    pass
    # code will go here

Inside this loop, we start off with the input() statement. The entered value gets saved into word, since that value is a word:

while True:
    word = input('Enter a hyphenated word: ')

If word is hyphenated, we break out of the loop but obviously after making the required output.

For the output, word is split into a list, at each hyphen (-) using word.split('-') and then each element of this list is printed using a for loop, after printing a blank line:

while True:
    word = input('Enter a hyphenated word: ')

    if '-' in word:
        words = word.split('-')
        print() # a blank line
        for w in words:
            print(w)
        break

If word isn't hyphenated i.e. the if conditional fails, we print 'Invalid input!' followed by a blank line.

while True:
    word = input('Enter a hyphenated word: ')

    if '-' in word:
        words = word.split('-')
        print() # a blank line
        for w in words:
            print(w)
        break
    
    else:
        print('Invalid input')
        print() # a blank line

There is no need to break the loop in the else block, since we have to repeat the entire code asking the user to reenter a hyphenated word.

This completes our exercise!