Pig latin string conversion in python

Question:

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"
Asked By: inkblot

||

Answers:

Works for me

def pig_latin(sentence):
    output = []
    for word in sentence.split(" "):
        word = word[1:] + word[0] + "ay"
        output.append(word)
    return " ".join(output)
Answered By: ventaquil

Just use a list comprehension on each word then join it back together.

edit: this solution have issues with multiple spaces, like when people put two spaces after a colon: !

def dowork(sentence):

    def pigword(word):
        return "".join([word[1:], word[0], "ay"])

    return " ".join([pigword(word) for word in sentence.split()])


dataexp = [
    ("hello how are you","ellohay owhay reaay ouyay"),
    ("programming in python is fun","rogrammingpay niay ythonpay siay unfay")
    ]

for inp, exp in dataexp:
    got = dowork(inp)
    msg = "exp :%s:  for %s n      got :%s:" % (exp, inp, got)
    if exp == got:
        print("good! %s" % msg)
    else:
        print("bad ! %s" % msg)

output

good! exp :ellohay owhay reaay ouyay:  for hello how are you
      got :ellohay owhay reaay ouyay:
good! exp :rogrammingpay niay ythonpay siay unfay:  for programming in python is fun
      got :rogrammingpay niay ythonpay siay unfay:
Answered By: JL Peyret
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

Answered By: emremrah

Here is a solution that worked in my case:

   def pig_latin(text):
      # Separate the text into words
        words = text.split()
        #creating a new empty list called pig
        pig=[]
        # creating a for loop to alter every word in words
        for word in words:
            
            """here we are assigning the altered word to a new variable pigged_word. The we can remove first word alone from the original word usin
                indexing and add/concat the letter at zeroth index (1st letter) back to the
                    word and add/concat new string 'ay' at end """
            
            pigged_word=word[1:]+word[0]+'ay'
            #append the altered words to the list
            # use append instead of insert since you can add word to end of list
            pig.append(pigged_word)
            #now convert it back to 
        return (' '.join(pig))
            
    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"

Kindly try to understand the question and solve. Splitting the say variable won’t work , ideally you’ll have to split the text into pieces and even then you don’t have to split with " "

Answered By: wonderboy13

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

Answered By: Hemang Singh

Based on Herman Singh’s answer, but omitting the anti-pattern (might be a bit strong to call it that) of creating an empty list and appending to it in a for-loop.

def pig_latin(text):
    words = [
        word[1:] + word[0] + "ay"
        for word in text.split()
    ]
    return " ".join(words)


print(pig_latin("hello how are you"))  # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun"))  # Should be "rogrammingpay n

Which can be reduced to a single line, but I think it doesn’t aid readability.

def pig_latin(text):
    return " ".join([word[1:] + word[0] + "ay" for word in text.split()])


print(pig_latin("hello how are you"))  # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun"))  # Should be "rogrammingpay siay unfay
Answered By: KeithWM
def pig_latin(text):
  # Separate the text into words
  words = text.split()
  newWord = []
  for word in words:
    # Create the pig latin word and add it to the list
    n = ""
    for char in range(len(word)-1, -1, -1):
      if(char == 0) :
        n+= (word[char]+"ay")
      n += word[char]
    newWord.append(n)    
    # Turn the list back into a phrase
  return " ".join(newWord)
    
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"
Answered By: J. Abdulrahman
def pig_latin(text):
  say = ""
  words = text.split()
  for word in words:
    word=word[1:] + word[0] + "ay" + " "
    say +=word
  return say
        
print(pig_latin("hello how are you"))
Answered By: RAKESH AGARWAL

Simply use a list comprehension for compactness and simplicity:

’ ‘.join([word[1:] + word[0] + ‘ay’ for word in text.split(‘ ‘)])

Answered By: Ryan Rudes

def pig_latin(text):

say = ""
  pig_list= []
  # 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"
    # print(word)
    pig_list.append(word)
    # Turn the list back into a phrase
    say = ' '.join(pig_list)
  return say
Answered By: Ahmed Osman

Solution without .join and .append:

def pig_latin(text):
  say = ""
  words = text.split()
  for word in words:
    pig_word = word[1:] + word[0] + "ay "
    say += pig_word
  return say
        
print(pig_latin("hello how are you")) # "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # "rogrammingpay niay ythonpay siay unfay"
Answered By: Eugene S.
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)
Answered By: Rohan Kumara
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 += " {}{}ay".format(word[1:], word[0])
    # Turn the list back into a phrase
  return 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"
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 += word[1:] + word[0] + "ay "
    # Turn the list back into a phrase
  return say.rstrip(" ")

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"
Answered By: zeal4learning
def pig_latin(text):
    say = ""
    str=[]
  # Separate the text into words
    words = text.split()
   for word in words:
     # Create the pig latin word and add it to the list
      say=word[1:]+word[:1]+"ay"
      str.append(say)
      # Turn the list back into a phrase
      joined=" ".join(str)
      return joined
    
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"
Answered By: Ghulam Yazdani
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 = say + word[1:] +word[0] +"ay "
    # 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"
Answered By: Vĩnh Nguyên Lâm

Using the following code:

def pig_latin(texts):
    return ' '.join([text.replace(text[0],'') + text[0]+'ay' for text in texts.split()])

print(pig_latin("hello how are you"))
print(pig_latin("programming in python is fun"))

will get the desired result:

ellohay owhay reaay ouyay
rogrammingpay niay ythonpay siay unfay
Answered By: apayziev
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+=word[1:]+word[0]+str("ay")+ " "
    # Turn the list back into a phrase
  return 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"
def pig_latin(text):
  say = ""
  words = text.split()
  for word in words:
    word = str(word[1])+str(word[2:] + str(word[0:1]).lower() + 'ay')
    say = say + word + " "
  return 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"
Answered By: Rohit Zoting
def pig_latin(text):
    say = "ay"
    # Separate the text into words
    words = text.split()
    newtext = []
    # Create the pig latin word and add it to the list
    for newword in words:
        newlist = newword[1:] + newword[0] + say
        newtext.append(newlist)
    return " ".join(newtext)
Answered By: user19096581
def pig_latin(text):
  say = ""
  # Separate the text into words
  words = text.split()
  last = words[-1]
  for word in words:
    if word == last:
        say += word[1:]+word[0]+"ay"
    else:
         say += word[1:]+word[0]+"ay "
  return 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"

Will get the output without that extra space at end of sentence.

Answered By: A.A

This is better work I think so as it is simple to understand

def pig_latin(text):
  # Separate the text into words
  words = text.split()
  list1 = []
  for word in words:
    # Create the pig latin word and add it to the list
    pig_latin = word[0] + "ay"
    mod_word = word.replace(word[0],"")
    mod_word2 = mod_word + pig_latin
    list1.append(mod_word2)
    # Turn the list back into a phrase
  return " ".join(list1)
        
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"

Answered By: Yogadharshan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.