Start learning free

LESSON 267 OF 673 · TUPLES, SORTING & BUILT-INS

Shout the Words in Python with a List Comprehension

Practice Read the full explanation

You get a list called words. Each word in it is in lowercase letters.

Return a new list with every word shouted.

To shout a word, write it in capital letters and put ! after it. So "hey" becomes "HEY!".

Keep the words in the same order.

 

Example 1:

Input: words = ["hey", "you"]
Output: ["HEY!", "YOU!"]
Explanation: Both words are in capital letters now.
Each one has a ! at the end.

Example 2:

Input: words = ["go"]
Output: ["GO!"]
Explanation: One word goes in, one word comes out.

Example 3:

Input: words = ["no", "no", "no"]
Output: ["NO!", "NO!", "NO!"]
Explanation: A word that repeats is shouted each time.

 

Constraints:

  • words has at least one word.
  • Every word has only the letters a to z.
STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · TUPLES, SORTING & BUILT-INS

    Shout The Words in Python, explained

    Problem overview

    The method shout gets a list words of lowercase words and returns a new list where every word is shouted: written in capital letters, with ! after it. So "hey" becomes "HEY!". The order stays the same, and a repeated word is shouted each time. For ["hey", "you"] the answer is ["HEY!", "YOU!"].

    Approach

    Every word turns into exactly one new word, in the same position, with no word skipped. That is the shape of a list comprehension without an if: one rule applied to each item. The rule has two parts. The string method upper() gives the word in capital letters, and + "!" adds the mark at the end.

    Strings can't be changed in place in Python, so word.upper() doesn't touch word. It returns a new string, and the comprehension collects those new strings in a new list.

    Step by step

    1. Build shouted with a comprehension: for each word in words, take word.upper() + "!".
    2. Return shouted. The input list words hasn't changed.

    The solution in Python

    class Solution:
        def shout(self, words: List[str]) -> List[str]:
            shouted = [word.upper() + "!" for word in words]
            return shouted
    

    The same loop written out is: start with an empty list, go through words, and append word.upper() + "!" each time. The comprehension is the short form of exactly that.

    Why not change the list in place

    The statement asks for a new list, and the comprehension gives exactly that. The caller's list is left alone, which is the safer habit: code that called shout(words) can keep using words afterwards and still see the lowercase originals. Also, a function that returns a value is easy to test, because you can compare its result to the expected list directly.

    Testing

    ["hey", "you"]    ->  ['HEY!', 'YOU!']
    ["go"]            ->  ['GO!']
    ["no", "no", "no"]  ->  ['NO!', 'NO!', 'NO!']
    

    After calling the method on ["hey"], the original list is still ['hey'].

    Other ways to build the string

    An f-string gives the same result: f"{word.upper()}!" produces 'HEY!' for "hey". Both are fine; the lesson uses + because it is the shortest way to read "add a ! after it". Be careful with similar methods: capitalize() only makes the first letter a capital, so "hey".capitalize() + "!" gives 'Hey!', which is not shouting. And leaving out the "!" gives ['HEY', 'YOU'], which is only half of the job.

    Edge cases

    A list with one word gives a list with one shouted word. A repeated word is shouted each time, because the comprehension handles each position on its own and never looks at the other words. The words have only the letters a to z, so upper() has no surprises here: every letter has a capital, and nothing else needs care. The result is always exactly as long as words.

    Common mistakes

    • Writing word.upper without the parentheses. That gives the method itself, not the capital letters, and adding "!" to it stops with a TypeError.
    • Calling words.upper() on the whole list, which stops with AttributeError: 'list' object has no attribute 'upper'. upper() belongs to a single string, so apply it to word.
    • Putting "!" in front, or adding it to the list instead of to each word.
    • Printing instead of returning. The method must return the new list.
    Keep going with Python500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free