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.
What is the netloc of https://example.com/docs?q=1?
Answer: example.com
Why: netloc is the network location, usually the host and optional port.
What is the output?
from urllib.parse import urljoin
print(urljoin("https://example.com/a/b.html", "../c.html"))Answer: https://example.com/c.html
Why: urljoin resolves ../ from the /a/ directory to the site root.
What does parse_qs return for page=2?
Answer: ['2']
Why: parse_qs maps each key to a list because keys can repeat.
What does this code print?
from urllib.parse import urlencode
print(urlencode({"q": "a b", "page": 2}))Answer: q=a+b&page=2
Why: urlencode encodes a space as + in a query string.
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.
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.
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.
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.
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.
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.
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.
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.