+ 1

Why we need line 9 and " " at end of line 11?

1. Phone = input("Phone: ") 2. digits_mapping = { 3. "1": "one", 4. "2": "two", 5. "3": "three", 6. "4": "four" 7. } 8. 9. output = "" 10. for character in phone: 11. output += digits_mapping.get(character, "!") + " " 12. print(output) Hi, This is piece code that if we give number, it basically convert it to word, Can anyone explain what is the reason of line 9 and that " " at the end of line 11?

14th May 2021, 1:52 PM
Tori
5 Réponses
+ 6
In line 9 => declared a variable having empty string . In line 11 => adding values of the dictionary in every iteration using += . Final value of output = "one two three four"
14th May 2021, 1:57 PM
TOLUENE
TOLUENE - avatar
+ 4
Hi Tori , line 9: output = "" We are declaring a string variable output. How we know it's string type? By looking at the data stored in variable output. That is "" for string. line 11: + " "; we are separating our output by space. eg: one two three four I hope this helps! Ping me if you still have doubts!
14th May 2021, 1:58 PM
minirkk
minirkk - avatar
+ 2
Tori we are putting empty string("") because we want our variable output to be of string type. Just like you put 0 for integers, 0.0 for float or true/false for boolean type.
14th May 2021, 2:23 PM
minirkk
minirkk - avatar
+ 2
Read this, you will be clear about static vs dynamic type checking. https://docs.oracle.com/cd/E57471_01/bigData.100/extensions_bdd/src/cext_transform_typing.html#:~:text=There%20are%20two%20main%20differences,type%20checking%20at%20compile%20time.
14th May 2021, 2:26 PM
minirkk
minirkk - avatar
+ 1
Hey Md Sayed, I know that line 9 is a variable with a empty string but why though? Why we should put that in there?
14th May 2021, 2:15 PM
Tori