Sharing Solutions

A while loop might work well here:

def add_exclamation(word):
    while len(word) < 20:
        word += '!'
    return word #, len(word) check

Figured this might work as well:

3 Likes

Two more solutions:
The simplest would be:

return word.ljust(20,"!")


Using a for loop also works:

 result = word
   for i in range(20-len(word)):
     result += "!"
   return result
2 Likes

Instead of creating a new string every time, which happens when you use for or while loops, I generated a string of exclamations of length = 20-len(word), which is required for β€˜word’ to reach 20 characters and added the string β€˜word’ to the string of exclamations.

def add_exclamation(word):
if len(word) >= 20:
return word

word += β€˜!’ * (20 - len(word))
return word

This is my code, I fill like i’m stupid )

Write your add_exclamation function here:

def add_exclamation(word):
new = β€œβ€
new_string = 0
if len(word) <= 20:
new_string = 20 - len(word)
new = β€œ!” * new_string
return word + new
return word