How to print a linebreak in a python function?
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.
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!
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.
참고URL : https://stackoverflow.com/questions/5982206/how-to-print-a-linebreak-in-a-python-function
'Nice programing' 카테고리의 다른 글
How to display loading image while actual image is downloading [duplicate] (0) | 2020.09.25 |
---|---|
Text size of android design TabLayout tabs (0) | 2020.09.25 |
Why do we need to use flatMap? (0) | 2020.09.25 |
Set selected index of an Android RadioGroup (0) | 2020.09.25 |
How to Install Sublime Text 3 using Homebrew (0) | 2020.09.25 |