How to Build a URL Query String in Python with urlencode
When a program calls a web address with parameters, such as ?q=weather+report&page=2, the values have to be escaped: a space or an & inside a value would otherwise break the address. urllib.parse.urlencode builds that text from a dictionary and does the escaping for you. This guide uses it on Python 3.15.2.
Syntax
from urllib.parse import urlencode
query = urlencode({"<name>": <value>, "<name>": <value>})
url = f"<address>?{query}"
| Part | What it means |
|---|---|
{"<name>": <value>} | The parameters, as a dictionary. Values can be text or numbers |
urlencode(...) | Returns them as one escaped text: name=value&name=value |
? | Separates the address from the query string when you join them |
Python's urllib.parse documentation says it converts "a mapping object or a sequence of two-element tuples" to "a percent-encoded ASCII text string".
The Example
The script builds the query for a search page from two parameters, adds it to an address, and then encodes values that need escaping: an & and a letter with an accent.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
from urllib.parse import urlencode
params = {"q": "weather report", "page": 2}
query = urlencode(params)
print(query)
print(f"https://example.com/search?{query}")
print(urlencode({"note": "rock & roll", "city": "São Paulo"}))

Step 2: Run It
uv run python urlencode_demo.py
q=weather+report&page=2
https://example.com/search?q=weather+report&page=2
note=rock+%26+roll&city=S%C3%A3o+Paulo

Each space became +, and the number 2 became the text 2. In the last line, the & inside the note became %26, so it can't be mistaken for the & between parameters, and ã became %C3%A3, its UTF-8 bytes. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Passing a ready-made string: urlencode("q=weather report") | TypeError: not a valid non-string sequence or mapping object | Pass the parameters as a dictionary |
Gluing values in with an f-string: f"?q={text}" | A space or & in text breaks the address or splits the parameter | Let urlencode build the query |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The server sees + where you sent a space | The server doesn't decode + as a space | Use urlencode(params, quote_via=quote) with from urllib.parse import quote, which writes %20 |
TypeError: not a valid non-string sequence or mapping object | The argument is a string | Pass a dictionary or a list of pairs |
FAQ
How do I get %20 instead of + for spaces?
Pass quote_via=quote: urlencode({"q": "weather report"}, quote_via=quote) gives q=weather%20report.
Can a parameter appear twice, as in ?tag=a&tag=b?
Yes. Give a list and doseq=True: urlencode({"tag": ["a", "b"]}, doseq=True) gives tag=a&tag=b.
Conclusion
urlencode({"q": "weather report", "page": 2}) gave q=weather+report&page=2, and escaped an & and an accented letter on its own. Build query strings with it instead of joining text.
Additional Resources
- urllib.parse.urlencode — every option, including
doseqandquote_via.
