+4

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

Naveen Kumar

11 Answers

New Answer

+16

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

+10

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

+6

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 : https://code.sololearn.com/cUo8IKPfns84/?ref=app

+2

Code playground has my answer.

+2

Thanks you Lothar

+1

@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!?

+1

Lothar I got it , Thanks you :)

+1

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!

+1

Stan Berger now I understand , thanks , stan

0

to the string

0

Thank you python programmer