FAQ: Code Challenge: String Methods - Add Exclamation

This community-built FAQ covers the “Add Exclamation” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Add Exclamation

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Is a range different from an integer?

2 posts were split to a new topic: Sharing Solutions

4 posts were split to a new topic: Is this code the best it can be?

def add_exclamation(word):
  for i in range(0, (20 - len(word))):
    word += "!"
  return word
1 Like

this may be a silly question but if strings are immutable why are you able to add characters to them using +=?

Because that is assignment. We are not adding a character to that string, but to a copy of it as a single expression and then assigning it back to the variable as a new string.

Let’s say that a is immutable.

a = 'string'

Now we can create an expression that combines a with another string object.

a + 's'

We have not changed a, only used it as an operand in our expression.

print (a + 's')    #  strings

a += 's'    #  same as `a = a + 's'`

print (a)    #  strings

This code is avoiding loops, adding exclamation points in one go instead.

def add_exclamation(word): if len(word) >= 20: return word else: return word + "!" * (20 - len(word)) print(add_exclamation("Codecademy")) print(add_exclamation("Codecademy is the best place to learn"))