Pig latin string conversion in python
22,648
Solution 1
def pig_latin(text):
words = text.split()
pigged_text = []
for word in words:
word = word[1:] + word[0] + 'ay'
pigged_text.append(word)
return ' '.join(pigged_text)
print(pig_latin("hello how are you"))
Outputs: ellohay owhay reaay ouyay
Solution 2
I tried this and it worked for me
def pig_latin(text):
say = []
# Separate the text into words
words = text.split()
for word in words:
# Create the pig latin word and add it to the list
word = word[1:] + word[0] + "ay"
say.append(word)
# Turn the list back into a phrase
return " ".join(say)
print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
Open for suggestions
Solution 3
def pig_latin(text):
say = []
# Separate the text into words
words = text.split(" ")
for word in words:
# Create the pig latin word and add it to the list
say.append(word[1:]+word[0]+'ay')
# Turn the list back into a phrase
return " ".join(x for x in say)
Author by
inkblot
Updated on July 06, 2022Comments
-
inkblot almost 2 years
I'm trying to create a function that turns text into pig Latin: simple text transformation that modifies each word moving the first character to the end and appending "ay" to the end. But all I get is an empty list. Any tips?
def pig_latin(text): say = "" words = text.split() for word in words: endString = str(word[1]).upper()+str(word[2:]) them = endString, str(word[0:1]).lower(), 'ay' word = ''.join(them) return word print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay" print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
-
Chris about 4 yearsDo you want to return word instead of words? It might break anyway
-
emremrah about 4 yearsWhy do you split
say
instead oftext
? -
inkblot about 4 yearsI'm not sure I understand the question. If I return word I get an error: Error on line 13: print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay" Error on line 11: return word UnboundLocalError: local variable 'word' referenced before assignment
-
Daniel B. about 4 yearsAs it seems to me, you are probably splitting the wrong string in the third line. Isn't it that you intend to split string-variable 'text' into 'words', instead of the kinda empty string 'say'?
-
inkblot about 4 years@emremrah because say is the string I want to split isn't it? If I split text I get: you fun
-
inkblot about 4 yearsYes @DanielB. that is right. But don't I need an empty string to assign to any text?
-
emremrah about 4 yearsNo, actually
text
is the string that you want to split.say
is just an empty string, and you are not adding it any string in the following lines of code, so it remains empty. Please check out my answer, you will see it. -
Daniel B. about 4 yearsDepends on the concrete implementation, but in most cases that is correct. However, what you are essentially trying to do in the code fragment is to split a pretty empty string, and hence not the one you really intend to split. As I see it, you want to iterate over the individual words contained in the input string passed to your function 'pig_latin'. But that input string is contained in your input variable 'text'. Hence, you have to split the text sequence contained in the 'text' string-variable [by applying the .split() method to it] into 'words'.
-
emremrah about 4 yearsActually my answer is nearly identical as @ventaquil, I didn't noticed that. Since he/she answered first, you can select his/her answer as the accepted answer.
-
Karl Knechtel about 4 years"don't I need an empty string to assign to any text?" I don't think I understand what this is intended to mean, but I think you must be confused about how names work in Python. There is no such thing as initializing a variable in Python; the first time you assign is basically the same as any other.
-
-
Chris about 4 yearsWhy would you need a nested function? ' '.join([x[1:]+x[0]+'ay' for x in 'hello world'.split()])
-
JL Peyret about 4 yearswell, it doesn't have to be nested. but I like making it into its own function to separate concerns. to each his own.
-
emremrah about 4 yearsActually my answer is nearly identical as @ventaquil, I didn't noticed that. Since he/she answered first, you can select his/her answer as the accepted answer.
-
xKobalt over 3 yearsWelcome to StackOverflow! While this answer may solve the OP's issues, it's recommended to provide an explanation about the written code to make your answer more understandable for the community and also to improve its quality