Python 根据指定的数字长度生成数字?

Python 根据指定的数字长度生成数字?,python,python-3.x,Python,Python 3.x,我想根据指定的数字长度n生成数字x。例如: n = 3 x = n-digits length where each digit is the number n, thus 333. n = 2 x = n-digits length where each digit is the number n, thus 22. 最简单的方法是什么?您可以对字符串执行乘法: int(str(n) * n) 您可以迭代地执行此操作,如下所示: def generatenumber(N): R=

我想根据指定的数字长度n生成数字x。例如:

n = 3
x = n-digits length where each digit is the number n, thus 333.

n = 2
x = n-digits length where each digit is the number n, thus 22.

最简单的方法是什么?

您可以对字符串执行乘法:

int(str(n) * n)

您可以迭代地执行此操作,如下所示:

def generatenumber(N):
    R=0
    for i in range(0, N): R+=N * 10**i
    return R

您可以专门使用数字运算来实现这一点,而不必求助于字符串表示,也可以使用封闭形式,而不必使用任何循环或递归:
n*(10**n-1)//9

您到底尝试了什么?