URL decoding with Python
Published Date: September 9, 2022
Updated Date: September 9, 2022
Python as a language provides means to encode your strings. In Python 3+ you can URL encode any string using the quote() function included in the urllib.parse package. The quote()
function by default encodes using UTF-8
format.
How to encode a string or a URL in Python using the quote() function
>>> import urllib.parse
>>> query = 'Tööls Cönverters xyz URLEncoder@Python'
>>> urllib.parse.quote(query)
Output:
'T%C3%B6%C3%B6ls%20C%C3%B6nverters%20xyz%20URLEncoder%40Python'
By default the quote()
function will not parse/encode the /
character. You need to escape the character. For example:
urllib.parse.quote('/')
Output:
'/'
The quote()
function also accepts a named parameter called safe
, the default value for which is /
.
In order to encode /
you can pass the safe argument as an empty string.
urllib.parse.quote('/', safe='')
Output:
'/'
In Python 3+ you can URL decode any string using the unquote() function included in the urllib.parse package. The unquote()
function by default decodes using UTF-8
format.
How to decode a string or a URL in Python using the unquote() function
>>> import urllib.parse
>>> query = 'T%C3%B6%C3%B6ls%20C%C3%B6nverters%20xyz%20URLDecoder%40Python'
>>> urllib.parse.unquote(query)
Output:
'Tööls Cönverters xyz URLDecoder@Python'
Related Decoding techniques:
- URL decoding with Javascript
- URL decoding with Python
- URL decoding with Ruby
- URL decoding with Java
- URL decoding with Golang
Related Encoding techniques:
- URL encoding with Javascript
- URL encoding with Python
- URL encoding with Ruby
- URL encoding with Java
- URL encoding with Golang
There are many applications that require you to encode your URL into a format that the systems can understand. If you wish to encode a URL, check out our free online URL Encoder.
Some systems communicate with encoded URLs, so you may need to decode a URL. If you want to decode a URL online, we have our free online URL Decoder.