Add Line Numbers
Add line numbers to any text with customizable separator, starting number, step, and zero-padding.
About this tool
Line numbering is essential when sharing code snippets, documentation, or any text that requires reference points. Adding line numbers manually is tedious and error-prone, especially for large documents. This tool automatically prepends sequential numbers to each line, making it easy to discuss specific sections and maintain consistent formatting across different contexts.
Simply paste your text into the editor, customize the separator (colon, period, or parenthesis), set your starting number, and choose the increment step. The tool instantly applies numbering while preserving your original text structure. You can also add zero-padding to align numbers visually—useful for files with 100+ lines where single-digit line numbers would appear misaligned.
Developers often use this tool when pasting error logs for debugging, technical writers when preparing numbered instructions, and educators when creating annotated code examples. The flexibility to start from any number and increment by custom intervals makes it valuable for renumbering sections or creating secondary numbering schemes alongside existing line numbers.
Frequently Asked Questions
Code Implementation
def add_line_numbers(text, start=1, step=1, sep=". ", pad=False, skip_empty=False):
lines = text.split("\n")
total = sum(1 for l in lines if (not skip_empty or l.strip())) if skip_empty else len(lines)
max_num = start + (total - 1) * step
width = len(str(max_num))
num = start
result = []
for line in lines:
if skip_empty and not line.strip():
result.append(line)
continue
num_str = str(num).zfill(width) if pad else str(num)
result.append(f"{num_str}{sep}{line}")
num += step
return "\n".join(result)
text = """Hello world
This is line two
Fourth line here"""
print(add_line_numbers(text, start=1, step=1, sep=". ", pad=True))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.