Output string on mosh video

hi guys,

I need some quick help to understand a code here. so on Mosh video on programming on Python on using dictionaries. so on the code he used, which he called, an output string to store the result of the dictionary.get() method

video with exact timestamp: Python Tutorial - Python for Beginners [Full Course] - YouTube

here is the code to better understand

phone = input('>')

    digits_mapping = {'1' : 'one',
                  '2' : 'two',
                  '3' : 'three'
    }

    output = ''

    for ch in phone :
        output += digits_mapping.get(ch,'!')

    print(output)

so why did he define that (output = ‘’) in the first place why cant he just use a normal variable below the for loop why did he increment the digits_mapping.get(ch,’!’) to that output variable ?
when can we use such technique of increment results to an empty variable instead of just using a variable directly without defining with an empty string and then increment it to the results that we want because I tried to put a normal variable as shown below to store the value of digits_mapping.get(ch,’!’) but it only prints the last entered number and not all numbers.

phone = input('>')

digits_mapping = {'1' : 'one',
                  '2' : 'two',
                  '3' : 'three'}

for ch in phone :
       output = digits_mapping.get(ch,'!')

print(output)

`

Hi symen,

Since the goal of this code is to convert the digits to an equivalent text-based representation, you need to iterate through the entered terminal input, then the code will find the mapping in the defined dictionary and for each finding, the for loop shall append the found ch value to the output variable.

To achieve that, for each found ch, the value of it will concatenate with the variable again and again until no more similar ch in the dictionary are found.

There are two methods to achieve that:

Either:

output = output + digits_mapping.get(ch,’!’)

or

output += digits_mapping.get(ch,’!’) — The method used by the Author.

In your code, you used non of the above ways, instead, your code will keep overwriting the output variable again and again until the loop ends. That’s why you always get the last value of your terminal input number: