Pig Latin Transformation in Python

Princy Prakash
1 min readJul 15, 2023

--

EXPLANATION:

  1. Start the pig_it function with the input parameter text.
  2. Initialize an empty string variable called result to store the transformed text.
  3. Check if the input text is an empty string. If it is, return the empty string.
  4. Split the text into individual words using whitespace as the delimiter and store the result in the text variable. This creates a list of words.
  5. Iterate over each word w in the text list.
  6. Check if the word w consists only of alphabetic characters using the isalpha() method.
  • If the word is alphabetic:
  • a. Construct the transformed word r by concatenating the substring of w from index 1 to the end, the first character of w, and the string 'ay'. This moves the first character to the end of the word and adds 'ay' at the end.
  • b. Append a whitespace and the transformed word r to the result string. This adds the transformed word to the result with a whitespace separator.
  • If the word is not alphabetic:
  • a. Append the original word w to the result string. This adds the non-alphabetic word to the result as it is.
  1. After iterating through all the words, the result string contains the transformed text.
  2. Return the result string, which represents the text after applying the Pig Latin transformation.

--

--

Princy Prakash
Princy Prakash

Written by Princy Prakash

Hi I Am Princy. I recently discovered my passion for programming and excited to get deeper into the world of technologies also in problem-solving

No responses yet