width = str(len(str(len(lines))))
10th February 2004
The above monstrosity came up today while writing a function to add zero padded line numbers to a chunk of text:
def linenumbers(text):
"Add zero padded line numbers to text"
lines = text.split('\n')
# Find the maximum 'width' of the line count
width = str(len(str(len(lines))))
for i, line in enumerate(lines):
lines[i] = ("%0" + width + "d. %s") % (i + 1, line)
return '\n'.join(lines)
I think it has a pleasant kind of symmetry to it.
More recent articles
- Gemini 3.5 Flash: more expensive, but Google plan to use it for everything - 19th May 2026
- The last six months in LLMs in five minutes - 19th May 2026
- Notes on the xAI/Anthropic data center deal - 7th May 2026