Python Tutorial # 2: English to Pig Latin Translator

For this project we will be working with if statements and loops to convert English to Pig Latin. There are many different versions of Pig Latin, but the rules we’ll be working with are as follows:

  • If a word starts with a vowel, add “way” to the end:
    • apple = appleway
    • ocean = oceanway
  • Else, find the first vowel and move all the letters before it to the end of the word. Then add “ay”:
    • the = ethay
    • blind = indblay
    • programmer = ogrammerpray

This is a pretty classic project when your’e getting started with programming because you to complete it, you just have to use the built in methods of any language that work with strings and if statements.

First Steps: What should our input / output be?

The first thing you should do when starting a new project is figure what your inputs and outputs should be. This way you can skip the starring at a blank screen faze of any project, and also so you know what your’e working towards.

In this case our input is a string, either a sentence or one word, without any punctuation marks. This is because this version of Pig Latin is only meant to be spoken, not written. So punctuation is going to be conveyed through things like inflection or pauses from the speaker. Our output will be a string as well, and we will print it on the screen at the end.

First Few Lines of Code

I recommend that you don’t spend too long worrying about how your’e going to write your program. Obviously, you’ll want to plan a little bit to avoid the staring at a blank screen faze I mentioned before. But, once you start coding and putting things down, things will just start coming to you.

So let’s get our input / output down in code. The following lines of code are the first things I would write given what we’ve decided:

  1. input = input(“Enter A Sentence Or Word: “)
  2. output = “”

The reason the output is an empty string is because we’re not sure what the string will be, but we know it will be a string. We can always use concatenation to add to it later.

How to Deal With Sentences

When you think about it a sentence isn’t a string. A word is a string and a sentence is a collection of strings separated by spaces. If we could split the sentence up by the spaces, we would have an array of strings. Well, thats exactly what the .split() function does.

For example, let’s use the sentence “That’s a dog.” and assign it to x. If we want to turn that sentence into an array or words, or strings, we would say x.split(). This would return {“That’s”,”a”,”dog.”}. Notice that the last word also is has the period attached.

The .split() also works on single words as well. It looks like this:

x = “Word”

x.split() = {“Word”}

All this being said, our next two lines would look like this:

  1. input = input(“Enter A Sentence Or Word: “)
  2. for word in words:

The for loop allows us to work with the individual words instead of the whole array. Now that we have our sentences broken into words we need to work with the characters in them.

How to Find the First Vowel

Before we find the first vowel in a string, we have to teach the program what vowels are. We’ll do this by providing the vowels in a list.

  1. vowels = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}

The vowels are all capitalized because when we compare the letters in a string to them, we will capitalize each letter first. I’m doing this for two reasons. First, I think it will be good practice with the built in python functions. Second, it’s easier to compare each of the letters in the string once, rather than checking if it is capitalized or not every time. So this is how we will check each letter in the strings. Remember to indent because this is in the for loop.

  1.     charAt = 0
  2.     while charAt != len(word) and (word.upper()[charAt] in vowels) == False:

  3.         charAt += 1

Let’s break down the while loop. I could have used a for loop and an if statement to do the same thing, but I want the loop to stop once we actually find the vowel, not check every letter in the word. The charAt != len(word) part is to make sure we only try to check the letters in the word and not run out of letters. I could have used <, but again I would like to get some practice with different python functionalities. The (word.upper()[charAt] in vowels) == False is where the program checks if the letter at space charAt is a vowel. .upper() capitalizes the entire word, charAt is the index of the spot where we find the vowel and in vowels is just checking if the letter we are currently checking is in the array of vowels. We increase charAt by 1 if the letter at that spot is not a vowel. 

What to Do When We Find It

Im actually going to give you the code first here before explaining it, because what you do with the location of the first value is actually up to you. You CAN use my rules for Pig Latin, but try to design your own rules and implement those instead. This is how I implement the rules for this project. Again, this is in the for loop so indent.


  1.     if charAt == 0:
  2.         new_word = word + “way”
  3.     else:
  4.         new_word = word[charAt : len(word)] + word[0 : charAt] + “ay”

The first if statement checks if the vowel is at the beginning of the word. If it is, we just add “way” to the end of the word. The else branch is for words that start with consonants. It takes the part of the word before the first vowel ( word[0:charAt] ), and puts it after the rest of the word ( word[charAt : len(word)] ). Then it adds “ay”.

Putting It All Together

To finish up the code we have to put our output together. Remember that output variable with the empty string inside? We should add our words to that string when we are finished rearranging them. We’ll do that like this ( still in the for loop, so indent ).

  1.     output = output + ‘ ‘ + new_word

The ‘ ‘ in the middle is not an empty string. It’s actually a space from tapping the spacebar.

The last thing you should do is print the output to the screen ( outside of the for loop ).

  1. print(output)

This program can be upgraded and built upon to do a lot of things with strings. For example, you should try to make the program run until a specific keyword is entered, like “/quit”. Or, like I said before, try to implement  your own rules for Pig Latin. If you have any questions about this project or how to expand it, feel free to comment on this post. I’ll see you in the next one.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.