试图在我的打印结果周围加上引号

时间:2013-02-25 00:04:10

标签: python

file = open('english.txt','r') 
vowels = ('a', 'e', 'i', 'o', 'u')

for text in file:
    translated= []
    lines = text.split()
    print(' '.join(lines))
    print("Translated to pig latin becomes:")
    for words in lines:
        if words[0] in vowels:
            words = words + 'yay'
        else:
            while words[0] not in vowels:
                words = words[1:] + words[0]
            words = words + 'ay'
        translated.append(words)
    words = ' '.join(translated)
    print(words, '\n')

我得到的结果是:

this is a statement
Translated to pig latin becomes:
isthay isyay ayay atementstay 

its going through python
Translated to pig latin becomes:
itsyay oinggay oughthray onpythay 

and this is the result
Translated to pig latin becomes:
andyay isthay isyay ethay esultray 

the end
Translated to pig latin becomes:
ethay endyay 

我希望每个节中的第1行和第3行都有引号。 例如“结束”“ethay endyay”

谢谢!

5 个答案:

答案 0 :(得分:2)

只需添加引号:

print('"' + words + '"\n')

或使用格式:

print('"{}"\n'.format(words))

答案 1 :(得分:1)

试试这样:

print('"{}"\n'.format(words))

答案 2 :(得分:1)

只需用引号括起相应print()语句中的对象:

file = open('english.txt','r') 
vowels = ('a', 'e', 'i', 'o', 'u')

for text in file:
    translated= []
    lines = text.split()
    print('"'+' '.join(lines)+'"') # Here
    print("Translated to pig latin becomes:")
    for words in lines:
        if words[0] in vowels:
            words = words + 'yay'
        else:
            while words[0] not in vowels:
                words = words[1:] + words[0]
            words = words + 'ay'
        translated.append(words)
    words = ' '.join(translated)
    print('"'+words+'"', '\n') # And here

答案 3 :(得分:0)

您可以像这样添加引号:

 print('"', words, '"', '\n')

答案 4 :(得分:0)

def quoted(s):
    return '"%s"' % s

print(quoted(words), '\n')