[Python-bugs-list] string.zfill() mishandles empty string (PR#31)

sjmachin@lexicon.net sjmachin@lexicon.net
Sun, 18 Jul 1999 23:27:23 -0400 (EDT)


Full_Name: John Machin
Version: 1.5.2
OS: Win 95 & NT
Submission from: news.colonial.com.au (140.168.16.251)


[WIN95] C:\junk>c:\progra~1\python\python
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> string.zfill('',4)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "C:\Program Files\Python\Lib\string.py", line 478, in zfill
    if s[0] in ('-', '+'):
IndexError: string index out of range
>>>
Comment: this should return '0000'.
Suggested fix: 
After the line that says
   if n >= width: return s
add this line:
   if not n: return '0' * width