-1

I'm attempting to create a pig-latin translation program. It is part of the 100 projects on GitHub I am working through. I do not like to check solutions before I have given it a real effort.

Here is the code I have at the moment, and it does complete the translation, the problem though is that it outputs the translation with some unsightly quotes around the replaced letter.

words = raw_input("Enter some text to translate to pig latin: ")
print "You entered: ", words

#Now I need to break apart the words into a list
words = words.split(' ')

#Now words is a list, so I can manipulate each one using a loop

for i in words:
    if len(i) >= 3: #I only want to translate words greater than 3 characters
        i = i + "%ray" % (i[0]) #The magical translator!
        i = i[1:] #I want to print the translation, but without the first letter
        print i.strip("'")

When I run this program I get this result:

You entered: hello world
ello'h'ay
orld'w'ay

I don't know how to strip the quotations out of my translated words. I think I'll use a .join command next to recreate the translated sentence, but I'm at a roadblock right now.

I have tried:

i = i.strip("'")

but that did not work either. Thanks!

Harrison Boles
  • 733
  • 2
  • 6
  • 9
  • Why use `"%ray"` in the first place? Why not just do `i = i + i[0] + "ay"`? – Kevin Nov 07 '13 at 19:57
  • Did you see [this other post?](http://stackoverflow.com/questions/931092/reverse-a-string-in-python) How about: >>> 'hello world'[::-1] 'dlrow olleh' This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string. – Chad Dienhart Nov 07 '13 at 20:01
  • @Kevin both methods do exactly the same thing? – Harrison Boles Nov 07 '13 at 20:13
  • @chad Not sure what you're post is referencing at all. I'm not asking about reversing strings, I did that project yesterday. – Harrison Boles Nov 07 '13 at 20:13
  • @HarrisonBoles, are you asking me? I think the answer is, "no, they do not do the same thing". `print i[0]` prints `h`, and `print "%r" % i[0]` prints `'h'`. – Kevin Nov 07 '13 at 20:15
  • @Kevin that is true, but I was using %r incorrectly, once changed to %s it works exactly the same - it was from the answer I marked correct. – Harrison Boles Nov 07 '13 at 20:16

3 Answers3

2

You don't need to replace anything. It's the '%r' in the format string that is causing the problem.

Change this:

i = i + "%ray" % (i[0]) 

Into this:

i = i + "%say" % (i[0]) 

...and it will all be fine.

R Hyde
  • 10,301
  • 1
  • 32
  • 28
1

If the characters you want to remove aren't at the ends of the string, then str.strip won't work.

Instead, you should be using str.replace here:

print i.replace("'", "")

See a demonstration:

>>> 'aba'.strip('b')
'aba'
>>> 'aba'.replace('b', '')
'aa'
>>>
1

.strip() strips the margins of the string. Use .replace("'") instead

for i in words:
    if len(i) >= 3:
        i = i + "%ray" % (i[0])
        i = i[1:]
        print i.replace("'")

Output:

You entered: hello world
ellohay
orldway
That1Guy
  • 7,075
  • 4
  • 47
  • 59