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
- Notes from my appearance on the Software Misadventures Podcast - 10th September 2024
- Teresa T is name of the whale in Pillar Point Harbor near Half Moon Bay - 8th September 2024
- Calling LLMs from client-side JavaScript, converting PDFs to HTML + weeknotes - 6th September 2024