Summary: In this programming example, we will learn to convert a string or sentence into Pig Latin using Python programming.

What is Pig Latin?

It is said that Pig Latin is not any kind of language but is a language game that children use to speak in code. It is formed by altering the letters in a word.

Here’s how it works:

First, pick an English word. We’ll use dictionary.

Next, move the first consonant or consonant cluster to the end of the word: “ictionary-d”.

Now add “ay” to the end of the word: “ictionary-day”.

That’s all there is to it; you’ve formed a word in Pig Latin.

I recommend you to read more about Pig Latin on the wiki.

Now we have some idea what exactly is Pig Latin, so let’s try converting a whole sentence into its Pig Latin form.

sentence = input('Enter a Sentence: ').lower()
words = sentence.split()

for i, word in enumerate(words):
    
    '''
    if first letter is a vowel
    '''
    if word[0] in 'aeiou':
        words[i] = words[i]+ "ay"
    else:
        '''
        else get vowel position and postfix all the consonants 
        present before that vowel to the end of the word along with "ay"
        '''
        has_vowel = False
        
        for j, letter in enumerate(word):
            if letter in 'aeiou':
                words[i] = word[j:] + word[:j] + "ay"
                has_vowel = True
                break

        #if the word doesn't have any vowel then simply postfix "ay"
        if(has_vowel == False):
            words[i] = words[i]+ "ay"

pig_latin = ' '.join(words)
print("Pig Latin: ",pig_latin)

Output:

Enter a Sentence: Pencil Programmer
Pig Latin: encilpay ogrammerpray

Explanation:

  1. Input a sentence using the input() method
  2. Split it into a list of words using split()
  3. Loop through the words and check if any of them begins with vowels.
    1. If yes, then concatenate “ay” and update the word in the word’s list.
    2. Else check if there is any vowel present in between the word.
      1. If yes then get the first vowel index position and postfix all the consonants present before that vowel to the end of the word along with “ay”.
  4. Otherwise, postfix “ay” to the word because it doesn’t have any vowel.
  5. Form a new sentence using the join() method and output it.

These are the steps following which we can easily convert any sentence into its corresponding Pig Latin form in Python.

This Post Has 9 Comments

  1. sarah

    when i try the above code, I have TypeError: ‘str’ object is not callable for line 6
    how do I convert the data?

  2. Adarsh Kumar

    The code is working fine. Please check again.

    1. Suzy

      Are you able show me how to pseudocode before coding this Pig Latin form in Python?

  3. Catalina

    how can i do this without using break?

  4. Suzy

    Can you pseudocode this in Python please?

    FOR EACH word IN words
    {
    APPEND(word, “-“)
    letter ← FIRST_LETTER(word)
    IF (IS_VOWEL(letter)) {
    APPEND(word, “yay”)
    } ELSE {
    APPEND(word, letter)
    APPEND(word, “ay”)
    REMOVE_FIRST(word)
    }
    }

    1. Adarsh Kumar
      for each word in words:
        append "-" to word
        letter = first letter of word
        if letter is a vowel:
          append "yay" to word
        else:
          append letter to word
          append "ay" to word
          remove first letter of word
      
  5. Suzy

    Thank you so much Adarsh Kumar!! I’m still in the basics.

  6. Suzy

    Hi Adarsh Kumar

    I need your help, can you explain this in text please? (bellow)

    # Returns true if the given character is an English vowel
    def is_vowel(char):
    vowels = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
    return char.lower() in vowels

    # Pig Latin algorithm
    words = [“peanut”, “butter”, “and”, “jelly”]
    for i in range(0, len(words)):
    word = words[i]
    word += “-“;
    first_letter = word[0]
    if is_vowel(first_letter):
    word += “yay”
    else:
    word += first_letter
    word += “ay”
    word = word[1:]
    words[i] = word

    print(words)

  7. ramu kaka

    thanks

Leave a Reply to Suzy Cancel reply