Explain Codes LogoExplain Codes Logo

How can I percent-encode URL parameters in Python?

python
url-encoding
python-3
urllib-parse
Alex KataevbyAlex Kataev·Jan 10, 2025
TLDR

A percent-encode URL parameter in Python can be handled via urllib.parse.quote. For transforming spaces into +, go ahead with urllib.parse.quote_plus.

  • Encoding with quote:

    from urllib.parse import quote print(quote('param with / & spaces')) # Why did the param cross the road? To encode spaces!🐥 # 'param%20with%20%2F%20%26%20spaces'
  • Encoding with quote_plus:

    from urllib.parse import quote_plus print(quote_plus('param with / & spaces')) # Convert spaces into '+', because why not?😜 # 'param+with+%2F+%26+spaces'

Managing Python versions & Unicode characters

Rely on Python 3 for an optimized experience with percent-encoding, as it accommodates Unicode handling. While you had to encode Unicode characters to UTF-8 in Python 2 (Oh, the good ol' times),Python 3 handles this like a boss.👩‍💼

  • Python 2: Encoding = Extra cash💰:

    print(urllib.quote(u'üníćødé'.encode('utf-8'))) # Life was tough back then mate!💪🏼 # %C3%BCn%C3%AD%C4%87%C3%B3d%C3%A9
  • Python 3: Native = Life's easy peasy 🥂:

    from urllib.parse import quote print(quote('üníćødé')) # Living life in Unicode # '%C3%BCn%C3%AD%C4%87%C3%B3d%C3%A9'

Empower safe parameter in quote while handling slashes '/'. Default it corresponds to '/', hence, won't encode

print(quote('param/with/slashes', safe='')) # 'param%2Fwith%2Fslashes'

The 'Safe' guide

It's high time we understood about safe which is default '/' while percent-encoding:

  • The untouchables: [A-Za-z], [0-9], and '_.-~' are exempted from percent-encoding.
  • Default safe links to '/'. To encode, try safe=''.
  • Embrace quote_plus for encoding spaces as '+', unlike ' '* encoded as **%20`* by quote.

Exploit urllib.parse.urlencode

When it comes to dealing with query string coupling multiple parameters, use urllib.parse.urlencode. It's your friend to build the query portion of a URL:

from urllib.parse import urlencode params = { 'name': 'John Doe', 'job': 'Developer/Coder' # When you can't decide on a job role } print(urlencode(params)) # Building block of URL # 'name=John+Doe&job=Developer%2FCoder'

This transforms dictionaries encoding both the keys and values, delivering a URL query.

Try alternative libraries

Python's urllib is the textbook for URL encoding. However, feel free to use other options for an enriched experience using external libraries or framework:

  • requests library:

    from requests.utils import quote
  • Django framework:

    from django.utils.http import urlquote # Django unchained

The art of decoding URLs

There are times when you might need to revert transformation, i.e., decode percent-encoded URLs. Pythons's urllib.parse.unquote is here for the rescue:

from urllib.parse import unquote print(unquote('param%20with%20%2F%20%26%20spaces')) # Back to the future # 'param with / & spaces'

Extract maximum from the docs

Make the most out of Python 3 urllib.parse documentation for everything about URL handling. A treasure of descriptions, syntax, and nuances, it can solve every use case that comes your way.