如何在python函数中打印linebreak ?

时间:2023-01-11 01:43:11

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.

但是/n不像换行符那样被识别。

6 个解决方案

#1


169  

You have your slash backwards, it should be "\n"

你把斜杠向后,应该是"\n"

#2


26  

The newline character is actually '\n'.

换行符实际上是'\n'。

#3


9  

for pair in zip(A, B):
    print ">"+'\n'.join(pair)

#4


6  

>>> A = ['a1', 'a2', 'a3']
>>> B = ['b1', 'b2', 'b3']

>>> for x in A:
        for i in B:
            print ">" + x + "\n" + i

Outputs:

输出:

>a1
b1
>a1
b2
>a1
b3
>a2
b1
>a2
b2
>a2
b3
>a3
b1
>a3
b2
>a3
b3

Notice that you are using /n which is not correct!

注意,您正在使用/n,这是不正确的!

#5


0  

\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.

\n是一个转义序列,由反斜杠表示。一个正常的正斜杠,例如/n将不能完成这项工作。在你的代码中,你使用/n而不是\n。

#6


0  

All three way you can use for newline character :

这三种方式都可以用于换行符:

'\n'

"\n"

"""\n"""

#1


169  

You have your slash backwards, it should be "\n"

你把斜杠向后,应该是"\n"

#2


26  

The newline character is actually '\n'.

换行符实际上是'\n'。

#3


9  

for pair in zip(A, B):
    print ">"+'\n'.join(pair)

#4


6  

>>> A = ['a1', 'a2', 'a3']
>>> B = ['b1', 'b2', 'b3']

>>> for x in A:
        for i in B:
            print ">" + x + "\n" + i

Outputs:

输出:

>a1
b1
>a1
b2
>a1
b3
>a2
b1
>a2
b2
>a2
b3
>a3
b1
>a3
b2
>a3
b3

Notice that you are using /n which is not correct!

注意,您正在使用/n,这是不正确的!

#5


0  

\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.

\n是一个转义序列,由反斜杠表示。一个正常的正斜杠,例如/n将不能完成这项工作。在你的代码中,你使用/n而不是\n。

#6


0  

All three way you can use for newline character :

这三种方式都可以用于换行符:

'\n'

"\n"

"""\n"""