DEV Community

Cover image for Pig Latin - with Python
Kwith Code
Kwith Code

Posted on

Pig Latin - with Python

Pig Latin Challenge

Looking for programming challenges? Try solo learn. Discover Amazing free courses, code bits and challenges.

Sololearn: Learn to Code

Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.

favicon sololearn.com

What is Pig Latin?

Latin is a playful form of word transformation used for amusement and secretive communication. It has absolutely no direct relations with the Latin Language. The basic rule is to shift a word's first consonant or consonant cluster the end and add 'ay'.

Coding a Pig Latin Converter

First, we'll begin with a list of the alphabetical letters of English and create a list of vowels

alpha = list(map(chr, range(97, 123))) vowels = ['a', 'e', 'i', 'o', 'u'] 
Enter fullscreen mode Exit fullscreen mode

In the first stage we'll work on converting a word to pig Latin.

Latinize the word function

Now we create a function taking in any word through a parameter,
create an empty list that will hold the first consonants of a word once we break it.

def latinize(word): firstCon = [] letters = list(word) 
Enter fullscreen mode Exit fullscreen mode

oh yeah, we'll need to break the word into a list in order to achieve our goal.

Next, create a list variable containing each letter of the word as an element. With the word converted to a list, we'll loop through the letters.

Create a 'for loop' looping through each letter. And in the loop, set a conditional statement to determine if the letter on iteration is a vowel or not using our vowel list.

def latinize(word): firstCon = [] letters = list(word) for letter in letters: if letter in vowels: 
Enter fullscreen mode Exit fullscreen mode

Within the if statement create another 'if' (nested if) to decide on whether that vowel is the first letter of the word. In the case that it is, return the word as it is.

create the 'else' (for the nested if) to break the loop. Why? because in case we bumped into a vowel of a word that should be the end of the loop.

In the else statement of the first 'if' conditional statement, append the letter to the 'first consonant' list.

def latinize(word): firstCon = [] letters = list(word) for letter in letters: if letter in vowels: if letters.index(letter) == 0: return word else: break else: firstCon.append(letter) 
Enter fullscreen mode Exit fullscreen mode

After that create another 'for loop', in this loop using the 'first consonant' list elements we'll move the first consonants from the front to the end of the letters by removing them and appending them.

def latinize(word): firstCon = [] letters = list(word) for letter in letters: if letter in vowels: if letters.index(letter) == 0: return word else: break else: firstCon.append(letter) for letter in firstCon: letters.remove(letter) letters.append(letter) 
Enter fullscreen mode Exit fullscreen mode

In the next step away from the loop we add 'ay' to the letters list and create a string variable to hold the latinized word.

And now to completely model the word back together, we'll loop through each element in the letters list and add it to the latinized word string.

Finally, we return the word as a string and our function is done.

def latinize(word): firstCon = [] letters = list(word) for letter in letters: if letter in vowels: if letters.index(letter) == 0: return word else: break else: firstCon.append(letter) for letter in firstCon: letters.remove(letter) letters.append(letter) letters.append('ay') latinized = '' for letter in letters: latinized += letter return latinized 
Enter fullscreen mode Exit fullscreen mode

At this point this seems like loads of work for a fun challenge but hang on
we just finished the main part. So, the good news is that you're almost done.

Follow for the next Part!

Test code:

print(latinize('Friday')) 
Enter fullscreen mode Exit fullscreen mode

full code:

Python Playground - Pig Latin Word.py: Online Interpreter, Compiler & Editor | Sololearn

Use our FREE Python online compiler to write, run and share your code. Works directly from your browser without any additional installation.

favicon sololearn.com

Top comments (0)