Pig Latin Translator

Jump to: navigation, search


  • You will create a program that translates English to Pig Latin


Translation Rules:

  1. If a word has no letters, don't translate it.
  2. Separate each word into two parts. The first part is called the prefix and extends from the beginning of the word up to, but not including, the first vowel. (The letter y will be considered a vowel.) The rest of the word is called the stem.
  3. The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters ay to the end. For example, sandwich is composed of s + andwich + ay + . and would translate to andwichsay.
  4. If the word contains no consonants, let the prefix be empty and the word be the stem. The word ending should be yay instead of merely ay. For example, I would be Iyay.


  1. Your first task is to produce a function named getPrefix(s) that takes one parameter, a string, and returns the portion of the word up to, but not including the first vowel. For example, if you sent 'banana' to your function, it should return 'b'. Sending 'Sibley' should return 'S', 'stream' should return 'str', and 'break' should return 'br'. Print out your working function and a sample run.
    1. You will use a loop to find the first vowel.
    2. Initialize a loop counter to start at 0
    3. Traverse the string s
      1. Inside the loop, check if the currently indexed letter matches the vowels (this will have a lot of == and or)
      2. If you find a vowel, then return string slice from 0 up to the index
    4. Outside of the loop return an empty string (no vowels were found if this line of code runs)
  2. Your next task is to produce a function named getStem(s) that takes one parameter, a string, and returns the stem portion of the word including the first vowel.
    • You should use a loop to find the first vowel. After that, return the string slice from the index of the vowel to the end of the string (len(str))
    • Hint: getStem(s) is very similar to getPrefix(s)
  3. Create a function named translateWord(s) that takes one parameter, a string s
    1. Use function calls to getPrefix(s) and getStem(s) to return the Pig Latin translated word.
    2. You should also be sure that you follow the rules of Pig Latin listed above (e.g. If the word contains no consonants, just add yay)


--> stop
--> littering
--> buddy


def translateWord(s):
      >>> translateWord("hello")
      >>> translateWord("bob")
      >>> translateWord("a")
      >>> translateWord("washington-lee")
      >>> translateWord("pants")
      >>> translateWord("littering")
      >>> translateWord("buddy")

if __name__ == '__main__':
    import doctest

Additional String Features:

  • Python strings actually have methods/functions built into them. You can read them at Python String Methods. Pay close attention to the string.split() and string.lower() functions/methods
  • Try out the following code:
s = "Here is a string with several words in it"
wordList = s.split()

#What do you see in wordList?
#Try out the same code, but using s.lower() this time...what is the difference?

Complete the Pig Latin Translator:

  1. Create a function called translateSentence(s) that takes one string parameter (it'll be a whole sentence)
  2. First, split the string into a list of words
  3. Traverse the list of words and call translateWord() on each word
  4. If you do it properly, you'll be able to translate an entire sentence into Pig Latin!