Converting to PigLatin with Python

0 min read 117 words

The challenge

Move the first letter of each word to the end of it, then add “ay” to the end of the word. Leave punctuation marks untouched. This string manipulation challenge is similar to other Python exercises like counting smiley faces or finding the intersection of two arrays .

Examples

pig_it('Pig latin is cool') # igPay atinlay siay oolcay
pig_it('Hello world !')     # elloHay orldway !

Test cases

Test.assert_equals(pig_it('Pig latin is cool'),'igPay atinlay siay oolcay')
Test.assert_equals(pig_it('This is my string'),'hisTay siay ymay tringsay')

How to write the code in Python

def pig_it(text):
    words = text.split(" ")
    
    new_words = []
    
    for word in words:
        if word.isalpha():
            new_word = word[1:] + word[0] + "ay"
            new_words.append(new_word)
        else:
            new_words.append(word)
        
        
    return " ".join(new_words)
    
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags