python中\ n的替代是什么?

时间:2016-09-28 02:30:13

标签: python newline end-of-line

我想在python中打印以下输出:

 99 buckets of bits on the bus.
 99 buckets of bits.
 Take one down, short it to ground.
 98 buckets of bits on the bus.

只有一个打印命令,中间没有“\ n”字符。怎么办呢?

3 个答案:

答案 0 :(得分:5)

使用三引号字符串文字,您可以使用实际换行符而不是\n

print(""" 99 buckets of bits on the bus.
 99 buckets of bits.
 Take one down, short it to ground.
 98 buckets of bits on the bus.""")

答案 1 :(得分:3)

import os
print os.linesep.join(["99 buckets of bits on the bus.",
 "99 buckets of bits.",
 "Take one down, short it to ground.",
 "98 buckets of bits on the bus."])

答案 2 :(得分:0)

这似乎有效:

print chr(10).join([
    '99 buckets of bits on the bus.',
    '99 buckets of bits.',
    'Take one down, short it to ground.',
    '98 buckets of bits on the bus.'
])