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 2.0 Flash: An outstanding multi-modal LLM with a sci-fi streaming mode - 11th December 2024
- ChatGPT Canvas can make API requests now, but it's complicated - 10th December 2024
- I can now run a GPT-4 class model on my laptop - 9th December 2024