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"))