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)
Share:
22,648
inkblot
Author by

inkblot

Updated on July 06, 2022

Comments

  • inkblot
    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
      Chris about 4 years
      Do you want to return word instead of words? It might break anyway
    • emremrah
      emremrah about 4 years
      Why do you split say instead of text?
    • inkblot
      inkblot about 4 years
      I'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.
      Daniel B. about 4 years
      As 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
      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
      inkblot about 4 years
      Yes @DanielB. that is right. But don't I need an empty string to assign to any text?
    • emremrah
      emremrah about 4 years
      No, 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.
      Daniel B. about 4 years
      Depends 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
      emremrah about 4 years
      Actually 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
      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
    Chris about 4 years
    Why would you need a nested function? ' '.join([x[1:]+x[0]+'ay' for x in 'hello world'.split()])
  • JL Peyret
    JL Peyret about 4 years
    well, it doesn't have to be nested. but I like making it into its own function to separate concerns. to each his own.
  • emremrah
    emremrah about 4 years
    Actually 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
    xKobalt over 3 years
    Welcome 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