---
source: https://iinventedlight.com/library/notes/python-urllib-parse.md
title: "Python urllib.parse splits, joins and percent-encodes URLs and query strings"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, urllib.parse — Parse URLs into components; python/cpython (GitHub), Lib/urllib/parse.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: urllib.parse"
tags: [python, urllib, url, query-string, percent-encoding, stdlib]
client: Librarian
sources:
  - id: S1
    title: "urllib.parse — Parse URLs into components"
    publisher: Python docs
    url: https://docs.python.org/3/library/urllib.parse.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/urllib/parse.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/urllib/parse.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python urllib.parse splits, joins and percent-encodes URLs and query strings
**Answer:** `urlsplit()` parses a URL into five components (scheme, netloc, path, query, fragment).

## Examples

```python
from urllib.parse import urlsplit, parse_qs, urlencode, quote, quote_plus, urljoin

parts = urlsplit("https://example.com/a/b?q=1&q=2#top")
parts.scheme, parts.netloc, parts.path   # ('https', 'example.com', '/a/b')
parse_qs(parts.query)                    # {'q': ['1', '2']}

urlencode({"q": "hello world", "page": 2})   # 'q=hello+world&page=2'
quote("/El Niño/")                           # '/El%20Ni%C3%B1o/'
quote_plus("/El Niño/")                      # '%2FEl+Ni%C3%B1o%2F'
urljoin("http://www.cwi.nl/%7Eguido/Python.html", "FAQ.html")
# 'http://www.cwi.nl/%7Eguido/FAQ.html'
```

## Which function

- `urlsplit()` returns a 5-item named tuple matching `scheme://netloc/path?query#fragment`. `urlparse()` also splits off `params`; it is based on obsoleted RFCs, and `urlsplit()` should generally be used instead.
- `parse_qs()` returns unique variable names as keys and lists of values; blank values are ignored unless `keep_blank_values` is true.
- `quote_plus()` is the default quote_via function, so spaces in values are quoted as a `+` character. Encode the result to bytes before using it as POST data with `urlopen()`.
- `quote()` replaces special characters using the `%xx` escape, keeps `/` by default, and never quotes letters, digits and `_.-~`.

## Security notes

- If the second argument to `urljoin()` is an absolute URL (starting with `//` or `scheme://`), its hostname and/or scheme will be present in the result, so it is generally not secure to use `urljoin` with an attacker-controlled url.
- `urlsplit()` and `urlparse()` do not perform validation of inputs; on unusual input they may return empty strings or `None` instead of raising an exception.
- Out-of-range port numbers raise `ValueError` (since 3.6), and ASCII newline and tab characters are stripped from the URL (since 3.10).

## See also

- [python-json](https://iinventedlight.com/library/notes/python-json/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)
- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)

## Claims

- id: C1
  claim: urlsplit() should generally be used instead of urlparse().
  source: S1
  evidence: "urlsplit() should generally be used instead of urlparse() ."

- id: C2
  claim: urlsplit and urlparse do not validate inputs.
  source: S1
  evidence: "The urlsplit() and urlparse() APIs do not perform validation of inputs."

- id: C3
  claim: Using urljoin with an attacker-controlled url is generally not secure.
  source: S1
  evidence: "it is generally not secure to use urljoin with an attacker-controlled url"

- id: C4
  claim: urlsplit parses a URL into 5 components.
  source: S2
  evidence: "Parse a URL into 5 components"

## Sources

- [S1] urllib.parse — Parse URLs into components. Python docs. https://docs.python.org/3/library/urllib.parse.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/urllib/parse.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/urllib/parse.py . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, urllib.parse — Parse URLs into components; python/cpython (GitHub), Lib/urllib/parse.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
