Python 3
In Python 3, the quote and urlencode functionalities are located in the module urllib.parse
.
Some Examples:
(un)quote
urllib.parse.quote
Replace special characters in string using the %xx escape.
>>> import urllib.parse
>>> urllib.parse.quote('https://www.google.com/')
'https%3A//www.google.com/'
Similarly, to reverse this operation, use urllib.parse.unquote:
Replace %xx escapes by their single-character equivalent.
>>> import urllib.parse
>>> urllib.parse.unquote('https%3A//www.google.com/')
'https://www.google.com/'
(un)quote_plus
Like quote(), but also replace spaces by plus signs, as required for quoting HTML form values when building up a query string to go into a URL.
>>> import urllib.parse
>>> urllib.parse.quote_plus('https://www.google.com/')
'https%3A%2F%2Fwww.google.com%2F'
Similarly, to reverse this operation, use urllib.parse.unquote_plus:
Like unquote(), but also replace plus signs by spaces, as required for unquoting HTML form values.
>>> import urllib.parse
>>> urllib.parse.unquote_plus('https%3A%2F%2Fwww.google.com%2F')
'https://www.google.com/'
urlencode
>>> import urllib.parse
>>> d = {'url': 'https://google.com', 'event': 'someEvent'}
>>> urrlib.parse.urlencode(d)
'url=https%3A%2F%2Fgoogle.com&event=someEvent'