Tools Coverters

URL Encoding in Python - A Detailed Tutorial

URL Encoding in Python - A Detailed Tutorial Image

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:

'/'

Related Encoding techniques:

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.