如何在python中打印n次数字? [英] How do I print a number n times in python?

查看:128
本文介绍了如何在python中打印n次数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中打印 n 次数字?

How do I print a number n times in Python?

我可以像这样打印 'A' 5 次:

I can print 'A' 5 times like this:

print('A' * 5)AAAAA

但不是 10,像这样:

but not 10, like this:

打印(10 * 5)50

我希望答案是10 10 10 10 10

如何转义 print() 中的数学运算?

How do I escape the mathematical operation in print()?

我不是在问如何将 string '10' 打印 n 次,而是在问如何将 number 打印 n 次.

I am not asking how to print the string '10' n times, I am asking how to print the number 10 n times.

推荐答案

10*5 实际上做了乘法,结果是 50,但是当你做 '10 '*5* 运算符执行重复运算符,如您在 'A' * 5 中看到的示例

10*5 actually does multiplication and ends up giving you 50, but when you do '10 '*5, the * operator performs the repetition operator, as you see in 'A' * 5 for example

print('10 ' * 5)

输出将是 10 10 10 10 10

或者你可以通过str(num)将int显式转换为字符串并执行打印操作

Or you can explicitly convert the int to a string via str(num) and perform the print operation

print((str(10)+' ') * 5)

这篇关于如何在python中打印n次数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆