Pig Latin Transformation in Python
1 min readJul 15, 2023
EXPLANATION:
- Start the
pig_it
function with the input parametertext
. - Initialize an empty string variable called
result
to store the transformed text. - Check if the input
text
is an empty string. If it is, return the empty string. - Split the
text
into individual words using whitespace as the delimiter and store the result in thetext
variable. This creates a list of words. - Iterate over each word
w
in thetext
list. - Check if the word
w
consists only of alphabetic characters using theisalpha()
method.
- If the word is alphabetic:
- a. Construct the transformed word
r
by concatenating the substring ofw
from index 1 to the end, the first character ofw
, 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 theresult
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 theresult
string. This adds the non-alphabetic word to the result as it is.
- After iterating through all the words, the
result
string contains the transformed text. - Return the
result
string, which represents the text after applying the Pig Latin transformation.