简体   繁体   English

如何在像 hello world 这样的新行中打印 86?

[英]How can I print 86 in new line like hello world?

Var1 = "54"
Var2 = "32"
Print(2 * "hello world\n")
Print(2 * str(int(var1)+int(var2)))

在乘以 2 之前将换行符连接到字符串。

print(2 * (str(int(var1) + int(var2)) + "\n"))

first to sum 2 integers you should not put them between double quotes or single quotes like you did, in that case it will sum up as strings and results in '54' + '32' = 5432 and for the new line, print already do it for you.首先求和 2 个整数,你不应该像你那样把它们放在双引号或单引号之间,在这种情况下,它会总结为字符串并导致'54' + '32' = 5432并且对于新行,打印已经做给你。

x = 54
y = 32

print(x+y)

ouput输出

86

sorry I did not understand what you want the first time, now I got it, try this:对不起,我第一次不明白你想要什么,现在我明白了,试试这个:

x = 54
y = 32
z = x + y
print(str(z)+'\n'+str(z))

output输出

86
86

upvote and checkmark :-)赞成和复选标记:-)

python's print command will always print on a new line when it is invoked, but since you are using the "2 * value" you are not actually invoking the print twice, youre telling print to print 2 versions of the value. python 的打印命令在调用时将始终打印在新行上,但是由于您使用的是“2 * 值”,因此您实际上并没有调用打印两次,您是在告诉打印打印该值的 2 个版本。

With that, you can add a new line character after your 86 to make it show on 2 lines as shown below.这样,您可以在 86 之后添加一个新行字符,使其显示在 2 行上,如下所示。

Also it's preferential, but it makes things a lot easier to debug when you separate large actions out.它也是优先的,但是当您将大型操作分开时,它会使调试变得容易得多。 Casting strings to ints, adding the ints together, and printing all in one line is a lot to digest when debugging and makes it more difficult to understand what exactly went wrong.将字符串转换为整数,将整数相加并在一行中打印所有内容在调试时需要消化很多,并且更难以理解到底出了什么问题。 It adds a few lines but it makes everything much more readable.它添加了几行,但它使所有内容更具可读性。 At the very least, you could move the action to it's own function, especially if you will be calling it many times.至少,您可以将操作移动到它自己的函数中,尤其是在您多次调用它的情况下。

var1="54"
var2="32"

print(2 * "hello world\n")

int1 = int(var1)
int2 = int(var2)
added = int1 + int2
var_str = "%s\n"%(added)
print(2 * var_str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM