使用python打印相同字符串x次

使用python打印相同字符串x次,python,if-statement,python-2.7,count,Python,If Statement,Python 2.7,Count,你好,堆栈溢出 这个问题很可能有一个非常简单的解决方案 count1 = 12 count2 = 15 countdifference = count2 - count1 if countdifference > 0: print selection 因此,上面的代码是不言自明的,但是我如何修改此代码以打印选择x次,其中x=计数差?在本例中,它将是15-12,即=3,然后它将打印选择3次 如果您有任何帮助或意见,我们将不胜感激 print

你好,堆栈溢出

这个问题很可能有一个非常简单的解决方案

    count1 = 12
    count2 = 15
    countdifference = count2 - count1

    if countdifference > 0:
        print selection
因此,上面的代码是不言自明的,但是我如何修改此代码以打印选择x次,其中x=计数差?在本例中,它将是15-12,即=3,然后它将打印选择3次

如果您有任何帮助或意见,我们将不胜感激

print str(selection) * countdifference


可以使用乘数运算符:

print selection * countdifference
在python中,当您有一个字符串并将该字符串相乘时,它将返回另一个字符串,如下所示:

>>> test = "message"
>>> print test * 3
messagemessagemessage

这似乎太简单了,我来试一试:)或
范围(count1,count2)
,这样就不需要计算差值了。
>>> test = "message"
>>> print test * 3
messagemessagemessage