2013-03-21 26 views
3
for i in range(1, 27): 
    temp=str(i) 
    print '%s'(temp.zfill(3)) 

Traceback (most recent call last): 
    File "<ipython console>", line 3, in <module> 
TypeError: 'str' object is not callable 

調用我不知道爲什麼'海峽' 對象是不是在我的自行車

爲我所要的輸出是這樣的:

001 
002 

..

021 

。 ..

所以我用zfill。 但python告訴我它是「str對象不可調用」 如何解決?

+0

順便說一句,你可以簡單地'打印「%03D」%i',避免' temp'。 – 2013-03-21 09:37:45

回答

5

你缺少%

print '%s' % (temp.zfill(3)) 
     ^THIS 
+0

謝謝你,這是正確的答案。 – sikisis 2013-03-21 09:16:12

4
print '%s'(temp.zfill(3)) 

應該

print '%s' % temp.zfill(3) 

實際上沒有必要爲%s 你可以使用

print temp.zfill(3) 
+0

謝謝你的回答。 – sikisis 2013-03-21 09:15:49

1

由於@jamylak和@NPE表示您忘記了%運營商,您實際上並不需要它。

但是,如果你想要做字符串格式化,因爲它是優於使用%你應該考慮使用str.format

for i in range(1, 27): 
    print '{0:0{1}}'.format(i, 3) 
相關問題