Pig Latin Translator in Python

Problem:


Most words in Pig Latin end in “ay.” Use the rules below to translate normal English into Pig Latin.

  • If a word starts with a vowel add the word “way” at the end of the word. Example: Awesome = Awesome +way = Awesomeway
  • If a word starts with a consonant, (such as dog) or a consonant cluster (such as brush), simply take the consonant/consonant cluster and move it to the end of the word, adding the suffix ‘ay’ to the end of the word.

For example, ‘dog’ in Pig Latin becomes ‘og-day’ (because the leading consonant ‘d’ has been moved to the end of the word, leaving simply ‘og’ at the beginning, and the suffix ‘-ay’ has been appended to the ‘d’).Our other example was the word ‘brush’, which becomes ‘ush-bray’ in Pig Latin, by following the same rule.

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string1=input("Enter a Sentence: ").strip()
words=string1.split()
l_words=string1.lower().split()
pig_latin=[]
 
for word in l_words:
    if word[0] in 'aeiou':
        pig_latin.append(word+'way')
    else:
        vowel_pos=0
        for w in word:
            if w in 'aeiou':
                vowel_pos=word.index(w)
                break
            else:
                pass
        pig_latin.append(word[vowel_pos:]+word[:vowel_pos]+'ay')
 
for i in range (0,len(words)-1):
    if words[i].istitle():
        pig_latin[i]=pig_latin[i].title()
 
print("Pig Latin Version is: "+" ".join(pig_latin).strip())

Output:

1
2
3
4
5
6
7
8
Enter a Sentence: Pig Latin is hard to speak
Pig Latin Version is: Igpay Atinlay isway ardhay otay eakspay
 
Enter a Sentence: I don’t know
Pig Latin Version is: Iway on’tday owknay
 
Enter a Sentence: Shriman is a good Boy
Pig Latin Version is: Imanshray isway away oodgay oybay


Comments