2013-06-24 56 views
0

你好堆棧溢出,打印相同的字符串x次使用python

更可能有一個非常簡單的解決方案,這個問題。

count1 = 12 
    count2 = 15 
    countdifference = count2 - count1 

    if countdifference > 0: 
     print selection 

所以上面的代碼是自我解釋,但是我怎麼能修改這個代碼打印選擇x次,其中x =的countdiffernce?在這種情況下,它將是15-12,其中= 3,然後它將打印選擇3次。

任何幫助或意見,將不勝感激,親切的問候AEA

回答

5
print str(selection) * countdifference 

​​
+0

好吧,看起來太簡單了,我給它一個旋轉:) – AEA

+1

或'range(count1,count2)',不需要計算差異。 –

2

可以使用乘數操作:

print selection * countdifference 

在Python中,當你有一個字符串,你乘這個字符串它會返回另一個字符串,如下所示:

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