URL encoding
is often needed when you’re calling a remote api with additional query strings or path parameters. Any query string or path parameter placed in the URL must be properly URL encoded.
URL encoding is also required while preparing data for submission with
application/x-www-form-urlencoded
MIME type.
In this article, you’ll learn how to encode URL components in Python. Let’s get started!
URL Encoding query strings or form parameters in Python (3+)
In Python 3+, You can URL encode any string using the
quote()
function provided by
urllib.parse
package. The
quote()
function by default uses
UTF-8
encoding scheme.
Note that, the
quote()
function considers
/
character
safe
by default. That means, It doesn’t encode
/
character
-
>> urllib.parse.quote('/')
The quote() function accepts a named parameter called safe whose default value is /. If you want to encode / character as well, then you can do so by supplying an empty string in the safe parameter like this-
>>> urllib.parse.quote('/', safe='')'%2F'
Encoding space characters to plus sign (+) using quote_plus() function
The quote() function encodes space characters to %20. If you want to encode space characters to plus sign (+), then you can use another function named quote_plus provided by urllib.parse package.
You can encode multiple parameters at once using urllib.parse.urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.
Encoding multiple parameters at once where one parameter can have multiple values
The urlencode() function takes an optional argument called doseq. If your input can have multiple values for a single key, then you should set the doseq argument to True so that all the values are encoded properly -
In Python 2.x the quote(), quote_plus(), and urlencode() functions can be accessed directly from the urllib package. These functions were refactored into urllib.parse package in Python 3.
The following examples demonstrate how you can perform URL encoding in Python 2.x using the above functions.