Skip to main content

How to Build a URL Query String in Python with urlencode

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
from urllib.parse import urlencode

query = urlencode({"<name>": <value>, "<name>": <value>})
url = f"<address>?{query}"
PartWhat 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​

urlencode_demo.py
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"}))

The urlencode_demo.py script in a macOS-style code window: it encodes a dictionary with q set to weather report and page set to 2, prints the result and a full address built from it, then encodes a note of rock & roll and a city of São Paulo.

Step 2: Run It​

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

The terminal after uv run python urlencode_demo.py: q=weather+report&page=2, the full address with that query, and 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​

MistakeWhat happensDo this instead
Passing a ready-made string: urlencode("q=weather report")TypeError: not a valid non-string sequence or mapping objectPass 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 parameterLet urlencode build the query

Troubleshooting​

SymptomLikely causeFix
The server sees + where you sent a spaceThe server doesn't decode + as a spaceUse urlencode(params, quote_via=quote) with from urllib.parse import quote, which writes %20
TypeError: not a valid non-string sequence or mapping objectThe argument is a stringPass 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​