How to print a linebreak in a python function?

╄→гoц情女王★ 提交于 2019-11-28 04:02:32
Winston Ewert

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

The newline character is actually '\n'.

for pair in zip(A, B):
    print ">"+'\n'.join(pair)
>>> 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!

Varun_k

All three way you can use for newline character :

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

You can print a native linebreak using the standard os library

import os
with open('test.txt','w') as f:
    f.write(os.linesep)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!