URLDecoder

URL Decoding query strings or form parameters in Python

by Venelin K. · 3 MINS

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.

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.

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

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

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

Decoding plus sign to space using unquote_plus()

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

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

Decoding multiple query parameters at once

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

urllib.parse.parse_qs(): Parse query string into a dictionary

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

urllib.parse.parse_qsl(): Parse query string into a list of tuples

>>> import urllib.parse
>>> queryStr = 'name=John+Doe&phone=%2B919999999999'
>>> urllib.parse.parse_qsl(queryStr)
[('name', 'John Doe'), ('phone', '+919999999999')]

URL Decoding in Python 2.x

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

urllib.unquote()

>>> import urllib
>>> queryStr = 'Hell%C3%B6+W%C3%B6rld%40Python'
>>> urllib.unquote(queryStr)
'Hell\xc3\xb6+W\xc3\xb6rld@Python'

urllib.unquote_plus()

>>> import urllib
>>> queryStr = 'Hell%C3%B6+W%C3%B6rld%40Python'
>>> urllib.unquote_plus(queryStr)
'Hell\xc3\xb6 W\xc3\xb6rld@Python'

Also Read: How to URL Encode a String in Python

References

Share on social media