Parse a CSV Input Line

Medium ⏱ 12 min 45% acceptance ★★★★☆ 4.2
Write a function parse_csv_line(line) that simulates parsing one line of user-entered CSV input, such as what input() might return. Given a comma-separated string line, return a list of the trimmed (whitespace-stripped) fields.

Examples

Example 1
Input
parse_csv_line("  John , 25 ,  Delhi")
Output
['John', '25', 'Delhi']
Explanation

Each field is split on commas and stripped of surrounding whitespace.

Example 2
Input
parse_csv_line("a,b,c")
Output
['a', 'b', 'c']
Explanation

No extra whitespace to strip.

Constraints

  • line always has at least one field, comma-separated.

Topics

Input & Outputparsing user input

Companies

FlipkartWalmart

Hints

Hint 1

`line.split(",")` breaks the string into raw fields.

Hint 2

Use `.strip()` on each field, e.g. via a list comprehension.

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