How can I percent-encode URL parameters in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the correct tool for percent-encoding URL parameters is usually urllib.parse.urlencode. It handles query strings safely, escapes reserved characters, and avoids the common mistake of trying to build URLs with manual string concatenation.
The important detail is that URL encoding depends on context. Query parameters, path segments, and entire URLs are not encoded the same way, so using the right function matters.
Encode Query Parameters with urlencode
If you have key-value parameters for a query string, use urlencode:
Output:
Spaces become + by default because query strings commonly use form-style encoding. Reserved characters such as / are escaped when they appear inside parameter values.
To build a full URL:
Repeated Parameters with doseq=True
If a parameter can appear multiple times, pass a list and enable doseq=True:
Output:
Without doseq=True, the list would be encoded as a single string representation, which is usually not what APIs expect.
quote vs quote_plus
If you need to encode one value manually rather than a full dictionary, use quote or quote_plus.
Output:
Use quote_plus for query parameter values when + for spaces is acceptable. Use quote when you specifically want %20.
Query Parameters Are Not Path Segments
One common bug is using urlencode for a URL path. Query parameters and path segments follow different expectations.
For a path segment, use quote:
Output:
If you leave / in the safe set, it will remain a slash and split the path unexpectedly.
Full Example with requests
If you are using requests, you often do not need to call urlencode yourself. Pass the params argument and let the library encode it:
This is safer than concatenating the query string by hand and easier to maintain.
Unicode and Non-ASCII Characters
Percent-encoding also handles Unicode safely by first encoding characters as UTF-8 bytes.
Output looks like:
That is normal and correct. URLs carry the encoded byte representation, not raw Unicode characters in every context.
Common Pitfalls
The biggest pitfall is building query strings with string interpolation. That breaks as soon as a value contains spaces, ampersands, slashes, or non-ASCII characters.
Another mistake is using quote on an entire key=value&key2=value2 string. That usually over-encodes separators and produces a broken query string. Encode the individual values or use urlencode on the whole parameter mapping.
Developers also forget doseq=True for repeated parameters. Many web APIs expect repeated keys, not a Python list literal encoded into a string.
Finally, do not confuse query encoding with path encoding. urlencode is for query strings; quote is for path segments and other individual URL components.
Summary
- Use
urllib.parse.urlencodefor query parameter dictionaries. - Use
doseq=Truewhen parameter values are lists that should expand into repeated keys. - Use
quoteorquote_plusfor individual values when needed. - Query strings and path segments should not be encoded with the same helper blindly.
- Avoid manual string concatenation for URLs.
- Libraries like
requestscan often handle parameter encoding for you automatically.
Related reading
- How can I pivot a dataframe?
- How can I print all eli5.explain_weights results without ellipsis?
- How can I print bold text in Python?
- How can I print multiple things fixed text and/or variable values on the same line, all at once?
- How can I print multiple things on the same line, one at a time?
- How can I print variable and string on same line in Python?
- How can I profile a multithread program in Python?
- How can I raise exception in main thread when using eventlet.GreenPool.spawn
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.