How to urlencode a querystring 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 normal way to URL-encode a query string is urllib.parse.urlencode. It handles the percent-encoding rules for query parameters so you do not have to manually replace spaces, ampersands, or non-ASCII characters yourself.
Use urllib.parse.urlencode
The basic pattern looks like this:
Output:
This is exactly what you want for standard query-string construction. Spaces become + in the query component, and reserved characters are safely encoded.
Build a full URL from the encoded query string
Once you have the encoded query string, append it to the base URL:
That gives you a properly encoded URL without manual string escaping.
Handle repeated parameters with doseq=True
If a parameter can have multiple values, pass a list and enable doseq=True:
Output:
Without doseq=True, the list itself would be stringified in a way that is usually not what web frameworks expect.
Know the difference between quote and urlencode
urlencode is for whole query parameter mappings. quote is for encoding a single string fragment.
Example with quote:
If you already have key-value parameters, urlencode is the higher-level and more appropriate tool:
Using the right function matters because different parts of a URL have different encoding rules.
Character encoding and non-ASCII text
urlencode handles Unicode values too:
That percent-encodes the non-ASCII bytes safely for transport in the URL.
In most modern Python code, you do not need to hand-manage this encoding step unless you are interfacing with something extremely unusual.
If you need different space-handling behavior, remember that standard query encoding uses + for spaces. That is normal for query strings. If you are encoding a URL path segment instead, use the path-oriented quoting tools instead of forcing query-string rules onto the wrong part of the URL.
When a request library can do it for you
If you are making HTTP requests, libraries such as requests can build the query string automatically:
That is often cleaner than building the URL manually when your real goal is to make the request rather than manipulate the string yourself.
Common Pitfalls
The biggest mistake is manually replacing spaces or special characters instead of using urlencode. Manual escaping is error-prone and easy to get wrong.
Another common issue is forgetting doseq=True when a parameter has multiple values.
People also confuse query-string encoding with path encoding. urlencode is for parameter mappings, not for arbitrary full-URL manipulation.
Finally, do not double-encode values. If a parameter string is already encoded, passing it through urlencode again can produce incorrect results.
Summary
- Use
urllib.parse.urlencodefor standard Python query-string encoding. - Append the encoded string to the base URL when building URLs manually.
- Use
doseq=Truefor repeated parameters stored in lists. - Prefer
quoteonly for single string fragments, not whole parameter dictionaries. - Let higher-level HTTP libraries handle query-string encoding when they already support it.
Related reading
- How to use / directory separator in both Linux and Windows in Python?
- How to use a dot . to access members of dictionary?
- How to use adaboost with different base estimator in scikit-learn?
- How to use an asyncio loop inside another asyncio loop
- How to use await in a python lambda
- How to use AWS SDK C XRay in a AWS Lambda Layer implemented in C called by a Lambda function in Python?
- How to use awscli inside python script?
- How to use Boto3 pagination
.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.