var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29
print(100*(var1+var2))
在上面的python代码中,我得到的是单行输出输出
我想把它打印成多行。我该怎么做才能成功呢?我已经尝试使用n转义字符,但我没有得到所需的输出。
如果你的意图是打印相同的东西100次,我建议使用for
循环而不是字符串乘法;它将消耗更少的内存(并不是说6个字符字符串的100倍很多,但如果数字更大,它可能会变得很重要),并且它使意图对普通读者更明显:
var1 = "arun"
var2 = "86"
for _ in range(100):
print(var1+var2)
如果你真的想构造一个带有换行符的字符串,除了在乘法之前插入n
:
print(100*(var1+var2+'n'))
另一个选择是使用str.join
和列表乘法:
print('n'.join([var1+var2]*100))
或生成器表达式:
print('n'.join(var1+var2 for _ in range(100)))
或使用print()
中的sep
参数执行上述任一操作:
print(*(var1+var2 for _ in range(100)), sep='n')
添加n
字符:
var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29
print(100*(var1+var2+'n'))
根据您提到的代码,输出如下:
arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86arun86
根据我对问题的理解,期望的输出如下:
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
arun86
要获得上述输出,请使用"n"在您的代码中,如下所示:
var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29
print(100*(var1+var2+'n'))
还有,如果你需要"86"同样要在下一行打印,您可以使用下面的代码等等:
var1 = "arun"
var2 = "86"
var3 = 4
var4 = 29
print(100*(var1+"n"+var2+'n'))
并期望以下输出:
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86
arun
86