Editor

Pig Latin Translator in Python

Beginner · Text and words · about 30 minutes to build · 65 lines

Type an English sentence and get it back in Pig Latin, with capitals and punctuation kept where they were.

The program

Press Run and answer its questions in the box that appears under the code.

pig_latin_translator.py
"""Pig Latin translator: turn English sentences into Pig Latin."""
import re
VOWELS = "aeiou"
VOWEL_ENDING = "way" # apple -> appleway
CONSONANT_ENDING = "ay" # pig -> igpay
WORD_PATTERN = r"[A-Za-z]+(?:'[A-Za-z]+)?" # a word, maybe with an apostrophe
# --- Translating one word ---
def first_vowel(word):
"""Return where the first vowel sound starts in a word."""
lower = word.lower()
for i, letter in enumerate(lower):
if letter in VOWELS:
if letter == "u" and i > 0 and lower[i - 1] == "q":
continue # "qu" stays together: quiet -> ietquay
return i
if letter == "y" and i > 0:
return i # y after a consonant acts as a vowel: rhythm -> ythmrhay
return len(word) # no vowel at all, like "hmm"
def pig_latin_word(word):
"""Translate one word and copy its capital letters across."""
split = first_vowel(word)
if split == 0:
result = word + VOWEL_ENDING
else:
result = word[split:] + word[:split] + CONSONANT_ENDING
result = result.lower()
if word.isupper() and len(word) > 1:
return result.upper()
if word[0].isupper():
return result.capitalize()
return result
# --- Translating a sentence ---
def translate(sentence):
"""Translate every word, leaving spaces, numbers and punctuation alone."""
return re.sub(WORD_PATTERN, lambda m: pig_latin_word(m.group()), sentence)
def main():
"""Translate sentences until the user presses Enter on an empty line."""
print("Pig Latin translator")
print("Move the first consonants to the end and add 'ay': pig -> igpay.")
print("Words that start with a vowel get 'way': apple -> appleway.")
while True:
sentence = input("\nEnglish (or press Enter to quit): ").strip()
if not sentence:
print("Oodbyegay.")
break
if not re.search(WORD_PATTERN, sentence):
print("There are no words to translate in that.")
continue
print("Pig Latin:", translate(sentence))
if __name__ == "__main__":
main()

Open it in the full editor to save it, share it and keep building.

Sample run

What the program printed, and what was typed (in yellow).

Pig Latin translator
Move the first consonants to the end and add 'ay': pig -> igpay.
Words that start with a vowel get 'way': apple -> appleway.

English (or press Enter to quit): The quick brown fox jumps over the lazy dog.
Pig Latin: Ethay ickquay ownbray oxfay umpsjay overway ethay azylay ogday.

English (or press Enter to quit): Hello, World. I'm learning Python in 2026.
Pig Latin: Ellohay, Orldway. I'mway earninglay Ythonpay inway 2026.

English (or press Enter to quit): 123 ???
There are no words to translate in that.

English (or press Enter to quit): NASA sent a rhythm to my squad
Pig Latin: ASANAY entsay away ythmrhay otay ymay adsquay

English (or press Enter to quit): 
Oodbyegay.

How it is built

1. Finding the first vowel

The word is split at its first vowel. Two extra rules make it sound right: qu stays together (quiet becomes ietquay) and a y after the first letter acts as a vowel (rhythm becomes ythmrhay).

def first_vowel(word):
    """Return where the first vowel sound starts in a word."""
    lower = word.lower()
    for i, letter in enumerate(lower):
        if letter in VOWELS:
            if letter == "u" and i > 0 and lower[i - 1] == "q":
                continue  # "qu" stays together: quiet -> ietquay
            return i
        if letter == "y" and i > 0:
            return i  # y after a consonant acts as a vowel: rhythm -> ythmrhay
    return len(word)  # no vowel at all, like "hmm"

2. Translating one word

Slicing does the work: word[split:] is the part from the vowel on and word[:split] the consonants in front of it. The result is lower-cased, then given the same capitals as the original word.

def pig_latin_word(word):
    """Translate one word and copy its capital letters across."""
    split = first_vowel(word)
    if split == 0:
        result = word + VOWEL_ENDING
    else:
        result = word[split:] + word[:split] + CONSONANT_ENDING
    result = result.lower()
    if word.isupper() and len(word) > 1:
        return result.upper()
    if word[0].isupper():
        return result.capitalize()
    return result

3. Translating a sentence

re.sub() can take a function instead of a replacement string. Each word that matches WORD_PATTERN is passed to pig_latin_word, so spaces, digits and punctuation stay where they were.

def translate(sentence):
    """Translate every word, leaving spaces, numbers and punctuation alone."""
    return re.sub(WORD_PATTERN, lambda m: pig_latin_word(m.group()), sentence)

4. The main loop

The loop ends when the user presses Enter on an empty line. re.search() checks that the sentence has at least one word before translating it.

def main():
    """Translate sentences until the user presses Enter on an empty line."""
    print("Pig Latin translator")
    print("Move the first consonants to the end and add 'ay': pig -> igpay.")
    print("Words that start with a vowel get 'way': apple -> appleway.")
    while True:
        sentence = input("\nEnglish (or press Enter to quit): ").strip()
        if not sentence:
            print("Oodbyegay.")
            break
        if not re.search(WORD_PATTERN, sentence):
            print("There are no words to translate in that.")
            continue
        print("Pig Latin:", translate(sentence))

Make it bigger

  • Add a translator from Pig Latin back to English, and find out why it cannot always be sure of the answer.
  • Let the user choose the endings, such as yay instead of way for words that start with a vowel.
  • Keep hyphenated words like x-ray together as one word.
  • Translate a whole text file line by line.

What it practises