使用单个打印在单独的行中打印多个变量 [英] Printing multiple variables in a separate lines using a single print
问题描述
例如,我有两个变量 int1 = 5
和 int2 = 3
如何仅使用一个 print<在单独的行中打印两个整数/code> 没有类型转换为
str
.(类似于 C++ 中的以下内容:cout<<int1<<endl<<int2;
)
For example, I have two variables int1 = 5
and int2 = 3
How can I print both the integers in separate lines using only a single print
without typecasting to str
. (like the following in C++: cout<<int1<<endl<<int2;
)
推荐答案
在 python3 中:
In python3:
print(string1, string2, sep='\n')
在python2中:
print string1 + '\n' + string2
... 或 from __future__ import print_function
并使用 python3 的打印
... or from __future__ import print_function
and use python3's print
自从我的第一个回答以来,OP 已经通过变量类型更改编辑了问题.更新已更新问题的答案:
Since my first answer, OP has edited the question with a variable type change. Updating answer for the updated question:
如果你有一些整数,即 int1
和 int2
:
If you have some integers, namely int1
and int2
:
Python 3:
print(int1, int2, sep='\n')
Python 2:
print str(int1) + '\n' + str(int2)
或
from __future__ import print_function
print(int1, int2, sep='\n')
或
print '\n'.join([str(i) for i in [int1, int2]])
这篇关于使用单个打印在单独的行中打印多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!