Read Multiple Numbers from One Line

Easy ⏱ 8 min 82% acceptance ★★★★★ 4.8
A common competitive-programming pattern is reading several space-separated integers from one line of input, e.g. from input().split(). Write a function sum_of_line(line) that takes a string line of space-separated integers and returns their sum as an int.

Examples

Example 1
Input
sum_of_line("3 5 7")
Output
15
Explanation

3 + 5 + 7 = 15.

Example 2
Input
sum_of_line("10")
Output
10
Explanation

A single number on the line.

Constraints

  • line contains one or more space-separated integers, possibly with extra spaces.

Topics

Input & Outputparsing user input

Companies

GoogleMicrosoft

Hints

Hint 1

`line.split()` handles extra/multiple spaces automatically.

Hint 2

Map each token through int() before summing.

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