Encode URL in Python

Syntax

Python2

urllib.quote(string[, safe])

urllib.quote_plus(string[, safe])

Python3

urllib.parse.quote(string, safe='/', encoding=None, errors=None)

urllib.parse.quote_plus(string, safe='', encoding=None, errors=None)

Example:

quote_plus('/El Niño/')

Result:

%2FEl+Ni%C3%B1o%2F


Decode URL in Python

Syntax

Python2

urllib.unquote(string)

urllib.unquote_plus(string)

Python3

urllib.parse.unquote(string, encoding='utf-8', errors='replace')

urllib.parse.unquote_plus(string, encoding='utf-8', errors='replace')

Example 1:

unquote('/El%20Ni%C3%B1o/')

Result:

'/El Niño/'

Example 2:

unquote_plus('/El+Ni%C3%B1o/')

Result:

'/El Niño/'