因为整数的位数只是其表示方式(以 10 为基数)的副产品,所以您必须将其转换为字符串。x = 100y = 1298411291836199301x = str(x)target_len = len(str(y))while len(x) < target_len: x += x# Cut off the last loop if it goes over the# desired length, and turn it back into an intx = int(x[:target_len])# >>> x# 1001001001001001001
您可以使用x = 100y = 1298411291836199301n = len(str(y))x = str(x)m = len(x)multiplier = n // m + 1x = ''.join( # join an iterable of strings into a single string (x for _ in range(multiplier)) # generator expression that returns x multiple times )[:n] # truncate the final string to the exact desired lengthx = int(x)print(x)print(y)输出10010010010010010011298411291836199301