Answer key · 12 questions

Python URL parsing Exercises With Answers

All 12 questions for Python URL parsing in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python URL parsing, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What is the netloc of https://example.com/docs?q=1?

  1. q=1
  2. https
  3. /docs
  4. example.comcorrect

Answer: example.com

Why: netloc is the network location, usually the host and optional port.

02 · Multiple choice

What is the output?

from urllib.parse import urljoin
print(urljoin("https://example.com/a/b.html", "../c.html"))
  1. https://example.com/a/c.html
  2. ../c.html
  3. https://example.com/a/b/c.html
  4. https://example.com/c.htmlcorrect

Answer: https://example.com/c.html

Why: urljoin resolves ../ from the /a/ directory to the site root.

03 · Multiple choice

What does parse_qs return for page=2?

  1. ['2']correct
  2. '2'
  3. 2
  4. ('2',)

Answer: ['2']

Why: parse_qs maps each key to a list because keys can repeat.

04 · Multiple choice

What does this code print?

from urllib.parse import urlencode
print(urlencode({"q": "a b", "page": 2}))
  1. q=a+b&page=2correct
  2. a b,2
  3. q=a b&page=2
  4. q%3Da%2Bb

Answer: q=a+b&page=2

Why: urlencode encodes a space as + in a query string.

05 · Fill in the blank

Complete the URL component parser.

from urllib.parse import urlparse
url = ____("https://example.com/docs?q=1")
print(url.netloc)

Answer: urlparse

Why: urlparse splits a URL into named components.

06 · Fill in the blank

Complete the function that resolves a relative link.

from urllib.parse import urljoin
base = "https://example.com/docs/page.html"
print(____(base, "../guide.html"))

Answer: urljoin

Why: urljoin combines a base URL with a relative reference.

07 · Fill in the blank

Complete the query-string parser.

from urllib.parse import parse_qs
params = ____("page=2&tag=python")
print(params["page"])

Answer: parse_qs

Why: parse_qs returns a mapping of keys to lists of values.

08 · Fill in the blank

Complete the query-string encoder.

from urllib.parse import urlencode
print(____({"q": "a b", "page": 2}))

Answer: urlencode

Why: urlencode builds an escaped query string from a mapping.

09 · Predict output

What does the program output?

from urllib.parse import urlparse
print(urlparse("https://example.com/docs/page.html").netloc)

Answer: example.com

Why: The network location is the host name example.com.

10 · Predict output

What appears on the screen?

from urllib.parse import urlparse
print(urlparse("https://example.com/docs/page.html").path)

Answer: /docs/page.html

Why: The path component is /docs/page.html.

11 · Predict output

What is printed?

from urllib.parse import urljoin
print(urljoin("https://example.com/docs/page.html", "../guide.html"))

Answer: https://example.com/guide.html

Why: The parent of /docs/ is the site root, so the result is /guide.html.

12 · Predict output

What is the output?

from urllib.parse import urlparse
print(urlparse("https://example.com/search?q=python&page=2").query)

Answer: q=python&page=2

Why: The query component contains everything after the question mark.

Try the questions first

Open the practice page for Python URL parsing and answer the same 12 questions with instant feedback.

Practice this lesson