0Pricing
Competitive Programming Academy · Lesson

Read N Lines and Edge Cases

Loop over lines and handle trailing whitespace.

The N-Then-Lines Pattern

A classic input format gives a count N first, then N lines of data. Read the count, then loop exactly N times.

Read the Count First

Pull the first line and turn it into an integer with int(). That number controls how many more lines you read.

n = int(input())

All lessons in this course

  1. input() vs sys.stdin Showdown
  2. Parse Many Numbers on One Line
  3. Batch Output the Right Way
  4. Read N Lines and Edge Cases
← Back to Competitive Programming Academy