This code is trying to take every character after the first character in the string, and store it in the new word. In Python, this can either be accomplished with:
Either of these methods will result in the following:
input: "Hello, World"
result: "ello, World"
In context, it is necessary to store every character after the first one in a separate string from the first one, so the first character may be added to the end according to the rules of Pig Latin.
Applying this to piglatin…
pyg = 'ay' #piglatin suffix
original = raw_input('Enter a word: ') #Input... Legacy Python
word = original.lower() #lowercase input - avoids future case issues
if(len(original)> 0 and original.isalpha()): #check for issues first
first = word #first character of lowercase input
new_word = word[1:] #rest of the word
print(new_word+pyg+first) #print the result!
It should be noted that as of Python3,
raw_input() should be replaced with
In this code snippet, we take our input, translate it to lowercase, check that it isn’t an empty space, check that it has only letters, and then start processing it. If these checks fail, we print “Empty!”.
Inside of our string processing case, we grab the first letter, and store it, then the rest of the word, and store it. Finally, we print out the result: Concatenation of everything past the first word (
new_word), the pig latin suffix (
pyg), and our first letter (
first). Put together, this looks like