在python中,\n的替代项是什么?

2024-04-20 09:48:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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”字符。如何做到这一点?在


Tags: oftheto命令onit字符one
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."])

对于三引号字符串文本,您可以使用实际的换行,而不是\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.""")

这似乎有效:

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.'
])

相关问题 更多 >