Add Line Numbers to a Script

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.7
Write number_file(src, dst) that copies src to dst, prefixing each line with its 1-based number, a colon and a space — 1: first line. Keep original line content otherwise untouched.

Examples

Example 1
Input
number_file('poem.txt', 'numbered.txt')  # poem: 'rose\nlily\n'
Output
numbered.txt: '1: rose\n2: lily\n'
Explanation

Each line gains its position.

Constraints

  • 1-based numbering.
  • Format: number, colon, space, content.

Topics

File Handlingtransform

Companies

InfosysWiproIBM

Hints

Hint 1

enumerate(f, start=1).

Hint 2

Write to the destination inside the same with statement.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.