How to decode URLs in Python

URL decoding, as the name suggests, is the inverse operation of URL encoding. It is often needed when you’re reading query strings or form parameters received from a client.

HTML forms by default use application/x-www-form-urlencoded content type for sending parameters. You need to decode the received parameters before using them in your Python applications.

Similarly, Query strings containing unsafe and non-ASCII characters are encoded as well. You need to decode them before use.

In this article, you’ll learn how to decode/parse URL query strings or Form parameters in Python 3.x and Python 2.x.

Let’s get started!

URL Decoding query strings or form parameters in Python (3+)

In Python 3+, You can URL decode any string using the unquote() function provided by urllib.parse package. The unquote() function uses UTF-8 encoding by default.

Let’s see an example -

>>> import urllib.parse
>>> encodedStr = 'Hell%C3%B6%20W%C3%B6rld%40Python'
>>> urllib.parse.unquote(encodedStr)
'Hellö Wörld@Python'

Decoding plus sign (+) to space character

The unquote() function does not decode plus sign (+) -

>>> urllib.parse.unquote('My+name+is+Rajeev')
'My+name+is+Rajeev'

But if you’re working with HTML forms then you’ll need to replace plus sign (+) with space character because HTML forms use application/x-www-form-urlencoded MIME type which encodes space character to plus sign (+) instead of %20.

For replacing plus sign with space, you can use the unquote_plus() function of urllib.parse package -

>>> import urllib.parse
>>> encodedStr = 'My+name+is+Rajeev'
>>> urllib.parse.unquote_plus(encodedStr)
'My name is Rajeev'

URL Decoding multiple query strings at once

If you want to decode or parse multiple query strings of type application/x-www-form-urlencoded (e.g 'name=Rajeev+Singh&phone=%2B919999999999'), then you can use parse_qs or parse_qsl functions provided by urllib.parse package.

The parse_qs function returns a dictionary of key-value pairs whereas the parse_qsl function returns a list of (key, value) tuples.

parse_qs

>>> import urllib.parse
>>> queryStr = 'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'
>>> urllib.parse.parse_qs(queryStr)
{'name': ['Rajeev Singh'], 'phone': ['+919999999999', '+628888888888']}

parse_qsl

>>> import urllib.parse
>>> queryStr = 'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'
>>> urllib.parse.parse_qsl(queryStr)
[('name', 'Rajeev Singh'), ('phone', '+919999999999'), ('phone', '+628888888888')]

URL Decoding query strings or form parameters in Python 2.x

The quote() and unquote() functions in Python 2.x are part of the urllib package directly. You can use them like so -

  1. urllib.unquote()

    >>> import urllib
    >>> queryStr = 'Hello%20World%0A'
    >>> urllib.unquote(queryStr)
    'Hello World\n'
  2. urllib.unquote_plus()

    >>> import urllib
    >>> queryStr = 'My+name+is+Rajeev.%20Thank%20you'
    >>> urllib.unquote_plus(queryStr)
    'My name is Rajeev. Thank you'

Also Read: How to encode URLs in Python

References