pls explain this python code !!!!!! | Sololearn: Learn to code for FREE!


pls explain this python code !!!!!!

it is a translator program , can anyone explain me how the letters are replaced by the for loop and if statement ?? def trans(ph): translation = "" for letter in ph: if letter in "AEIOUaeiou": translation = translation + "N" else: translation = translation + letter return translation print(trans(input("enter a word : "))) Expecting your answer :) Thank you

11/12/2019 4:18:10 PM

Naveenkumar v

10 Answers

New Answer


It's not as complicated as it looks like. The input string is iterated in a for loop. All vowels will be replaced by *N*, all the rest will keep as input. enter a word : aeiouAEIOU # input NNNNNNNNNN # output enter a word : bcdfghBCDFGH # input bcdfghBCDFGH #output


Naveen Kumar, *translation* does nothing special, it's just the name of a (string) varible where the output string is composed by using concatenation.


Naveen Kumar, i forgot to mention, that there is a real function in python (translate()) that can do a translation with characters and a predefined dict. Here a sample :


Thanks you Lothar


@lothar can you pls explain me this line of code sir? if letter in "AEIOUaeiou": translation = translation + "N" else: translation = translation + letter how translation works!?


Lothar I got it , Thanks you :)


Naveen, think of translation as a building block variable. Its value starts as the empty string "" and each translation = translation + ? adds the defined letter the string (building block). Hope that helps!


Stan Berger now I understand , thanks , stan


to the string


Thank you python programmer