Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minuteUse urllib.parse.urlencode to turn query data into an ampersand-separated, percent-encoded string. Set doseq=True when a value is a sequence that should produce repeated keys, and choose quote_via to control how spaces and slashes are encoded.
Build a query string with urlencode
urlencode accepts either a mapping or a sequence of two-item tuples, where each tuple contains a key and its value. It returns percent-encoded ASCII text with pairs separated by &. For example:
from urllib.parse import urlencode
params = {"q": "python urls", "page": 2}
print(urlencode(params))
# q=python+urls&page=2
The default encoding uses form-style quoting: spaces become +, and a slash in a key or value is encoded as %2F. Both keys and values are passed through the selected quoting function.
Use doseq=True to repeat a key for sequence values
With the default doseq=False, a sequence supplied as a value is treated as one value rather than expanded into multiple query pairs. Set doseq=True when you want a separate pair for each element:
#1 Best Overall
from urllib.parse import urlencode
params = [("tag", ["python", "urls"]), ("page", 2)]
print(urlencode(params, doseq=True))
# tag=python&tag=urls&page=2
When the input is a sequence of tuples, the resulting pairs follow the tuple order. This makes tuple input useful when repeated keys or a specific parameter order matter.
Choose between quote_plus and quote
The quote_via argument selects the function that quotes keys and values. Its default is quote_plus. Pass urllib.parse.quote if you want spaces represented as %20 and slashes left unescaped by default:
Rank #2
from urllib.parse import quote, urlencode
params = {"path": "docs/python urls"}
print(urlencode(params))
# path=docs%2Fpython+urls
print(urlencode(params, quote_via=quote))
# path=docs/python%20urls
| Option | Space | Slash (/) |
Typical choice |
|---|---|---|---|
Default quote_plus |
+ |
%2F |
Use when form-style plus-for-space encoding is wanted. |
quote |
%20 |
Unescaped by default | Use when percent-encoded spaces are wanted and slashes should remain unescaped. |
The safe argument can further specify characters to leave unquoted. The safe, encoding, and errors arguments are passed to the quoting function; encoding and errors apply only to string query elements.
Parse the result back into Python data
Use urllib.parse.parse_qs or urllib.parse.parse_qsl to parse a query string. They are the documented counterparts for turning encoded query data back into Python structures. Choose the parser that fits the shape your code needs.
Free tools Windows power users keep installed
One-click scans. No signup required.
Python version notes
According to the Python library reference, urlencode has accepted bytes and string query values since Python 3.2, and quote_via was added in Python 3.5. The current documentation marks acceptance of certain false-valued objects as deprecated in Python 3.14, with exceptions including empty strings, byte-like objects, and None. Check the documentation for the specific Python release you support when relying on compatibility details.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




