2

我在 Win7 上使用 Python 3.2。我用 ASCII 码写了这个:

print (''.join((chr(i+22) for i in (50,75,90,90,99))))
print (''.join((chr(j+22) for j in (44,83,92,94,82,78,75,99,11))))

结果写道:

Happy 
Birthday!

现在,我想将这两个词加入一个句子中,所以它写道:

Happy Birthday!

这似乎是一件简单的事情,但我是 Python 新手,所以有人可以帮助我吗?谢谢 :)

4

5 回答 5

2

你的意思是这样吗?

print (''.join((chr(i+22) for i in (50,75,90,90,99,10,44,83,92,94,82,78,75,99,11))))
于 2013-07-31T11:40:31.350 回答
1

要将它们放在同一行,并且在第一个打印语句的末尾,请输入参数 end=" ",这样下一个打印语句将在同一行打印。

于 2013-07-31T16:33:23.397 回答
0

You can ask print() not to add a newline:

print(..., end='')

end, by default, is set to \n.

For your sample, that'd be:

print(''.join((chr(i+22) for i in (50,75,90,90,99))), end=' ')
print(''.join((chr(j+22) for j in (44,83,92,94,82,78,75,99,11))))

printing a space instead of a newline after Happy.

You could also include the space in your list of ASCII codepoints; ASCII space is 32, but you add 22 to your values, so including 10 should do it:

print(''.join((chr(i+22) for i in (50,75,90,90,99,10,44,83,92,94,82,78,75,99,11))))
于 2013-07-31T11:36:36.803 回答
0

它很简单..只需使用+运算符。

print (''.join((chr(i+22) for i in (50,75,90,90,99))))+" "+ (''.join((chr(j+22) for j in (44,83,92,94,82,78,75,99,11))))
于 2013-07-31T11:32:55.837 回答
0

使用字符串格式打印输出:

s1 = ''.join((chr(i+22) for i in (50,75,90,90,99)))
s2 = ''.join((chr(j+22) for j in (44,83,92,94,82,78,75,99,11))))

print("%s %s" % (s1, s2))
于 2013-07-31T11:40:16.967 回答