How to convert a sentence to Pig Latin in Python

Pig Latin is a pseudo-languageA pseudo-language is something written or spoken that resembles language but is not a true language. that's based entirely on English. Words for Pig Latin are formed by manipulating English words while conforming to a set of trivial rules. Since these rules are simple to follow, Pig Latin is mainly "spoken" by children when they want to converse in code or by adults interacting with children.

"Pig Latin" written in Pig Latin

Rules

The rules for Pig Latin, as previously said, are straightforward. These rules modify English words depending on whether they begin with a vowel or a consonant.

These words must be suffixed with “yay,” resulting in the Pig Latin alternative.

However, it must be noted that suffixes such as “way,” “hay,” or “ay” can also be used.

These initial consonant (or group of initial consonants) before the first vowel will be moved to the end of the word. Then, this new word is going to be suffixed with “ay.”

The initial consonant (or group of consonants) are highlighted in black in the illustration to the left.

For words that do not contain a vowel, they’re simply suffixed with “ay.” This is illustrated in the last example on the left.

Compound words must be broken down into their composing words. The vowel/consonant rules will then be applied on those composing words.

Code

The following is a code snippet incorporating all of the rules, but it doesn't cater to compound words as those are beyond the scope of written Pig Latin (since Pig Latin is mainly spoken and not written).

Note: To execute the following program, enter a sentence composed of English words, without any punctuation.

english_sentence = input().lower()
english_words = english_sentence.split()
latin_words = []
vowels = ['a','e','i','o','u']
for word in english_words:
has_vowel = False
for i in range(len(word)):
'''
if first letter is a vowel
'''
if word[0] in vowels:
latin_words.append(word + "yay")
break
else:
'''
else get vowel position and postfix all the consonants
present before that vowel to the end of the word along with "ay"
'''
if word[i] in vowels:
latin_words.append(word[i:] + word[:i] + "ay")
has_vowel = True
break
#if the word doesn't have any vowel then simply postfix "ay"
if(has_vowel == False and i == len(word)-1):
latin_words.append(word + "ay")
break
pig_latin_sentence = ' '.join(latin_words)
print(pig_latin_sentence)

Enter the input below

Explanation

In the code above:

  • Line 1: Converts the input string to lower case.

  • Line 2: Converts the string into a list of words.

  • Line 3: Create a list latin_words that will contain all the pig latin words.

  • Line 5: A list vowels contains all the vowels.

  • Lines 7: An outer loop that iterates through the whole list of words.

  • Line 10-32: An inner loop that Iterates through each letter of the word.

    • Line 15-17: If the first letter of the word is vowel, then append "yay" at the end, add the string in the list latin_words and break the inner loop.

    • Line 24-27: If vowel exists in the word but not on the first index, then the consonants before the first vowel are moved to the end and string "yay" after that. Add the string in the list latin_words and break the inner loop.

    • Line 30-32: If the string have no vowels, then just add "ay" at the end. Add the string in the list latin_words and break the inner loop.

  • Line 34: Join all the words in the latin_words list and make a sentence.

  • Line 35: Print the pig_latin_sentence sentence.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved