Back to Practice
#0195

Parse a Query String

MediumPython25 min20 XP

Problem

Given a query string like "page=2&sort=asc", return a dictionary of keys and values as strings.

Why This Matters

Query strings, environment variables, and config lines all require careful splitting around separators.

Function Signature

def parse_query(query):

Examples

Example 1
Input"page=2&sort=asc&topic=python"
Output{"page": "2", "sort": "asc", "topic": "python"}

Each pair is split at &, then each key and value is split at the first =.

Constraints

  • Return the exact requested result.
  • Handle common edge cases.
  • Keep the solution readable before optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input"page=2&sort=asc&topic=python"
Expected{"page": "2", "sort": "asc", "topic": "python"}

Each pair is split at &, then each key and value is split at the first =.

Hidden Test Categories
empty or minimal inputrepeated valueslarger realistic input