如何在Python函数中打印换行符?
我的代码中有一个字符串列表;
A = ['a1', 'a2', 'a3' ...]
B = ['b1', 'b2', 'b3' ...]
我想打印它们并用换行符分隔,如下所示:
>a1
b1
>a2
b2
>a3
b3
我已经尝试过:
print '>' + A + '/n' + B
但是 /n 不像换行符那样被识别。
I have a list of strings in my code;
A = ['a1', 'a2', 'a3' ...]
B = ['b1', 'b2', 'b3' ...]
and I want to print them separated by a linebreak, like this:
>a1
b1
>a2
b2
>a3
b3
I've tried:
print '>' + A + '/n' + B
But /n isn't recognized like a line break.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你的斜杠是向后的,它应该是
"\n"
You have your slash backwards, it should be
"\n"
换行符实际上是
'\n'
。The newline character is actually
'\n'
.您可以使用所有三种方式来换行符:
All three way you can use for newline character :
输出:
请注意,您使用的是
/n
,这是不正确的!Outputs:
Notice that you are using
/n
which is not correct!\n
是转义序列,用反斜杠表示。普通的正斜杠(例如/n
)无法完成此任务。在您的代码中,您使用/n
而不是\n
。\n
is an escape sequence, denoted by the backslash. A normal forward slash, such as/n
will not do the job. In your code you are using/n
instead of\n
.您可以使用标准 os 库打印本机换行符
You can print a native linebreak using the standard
os
library另外,如果您将其设为控制台程序,则可以执行以下操作:
print(" ")
并继续您的程序。我发现这是分离文本的最简单方法。Also if you're making it a console program, you can do:
print(" ")
and continue your program. I've found it the easiest way to separate my text.下面的
python 3.6
使用print(">%s\n%s 而不是
print(f">{a}\n{b}")
“%(a,b))Below
python 3.6
instead ofprint(f">{a}\n{b}")
useprint(">%s\n%s" % (a, b))