Python URL Encode & Decode

Encode and decode URLs online — with Python urllib.parse examples, gotchas, and tips.

🔒 100% private — runs entirely in your browser
or try sample data

URL Encoding in Python

Python's urllib.parse module provides functions for percent-encoding URLs and query parameters. It converts unsafe characters to %XX hex sequences so they can be safely included in URLs.

  • quote() — percent-encodes a string for use in URL path segments. Spaces become %20.
  • quote_plus() — encodes for query parameters (application/x-www-form-urlencoded). Spaces become +.
  • unquote() / unquote_plus() — decode percent-encoded strings back to readable text.
  • urlencode() — encodes a dictionary of key-value pairs into a query string.
  • requests library — the popular requests library handles URL encoding automatically via the params argument.

Python URL Encoding Code Examples

Basic URL Encoding with quote()

from urllib.parse import quote text = "hello world & more"
encoded = quote(text)
print(encoded) # hello%20world%20%26%20more

Query Parameter Encoding with quote_plus()

from urllib.parse import quote_plus query = "search term with spaces"
encoded = quote_plus(query)
print(encoded) # search+term+with+spaces

Encode a Dictionary as Query String

from urllib.parse import urlencode params = { "q": "python url encode", "page": 1, "lang": "en"
}
query_string = urlencode(params)
print(query_string) # q=python+url+encode&page=1&lang=en

URL Encoding with requests Library

import requests # requests handles URL encoding automatically
response = requests.get( "https://api.example.com/search", params={"q": "hello world", "lang": "en"}
)
print(response.url)
# https://api.example.com/search?q=hello+world&lang=en

Decode URL-Encoded Strings

from urllib.parse import unquote, unquote_plus encoded = "hello%20world%20%26%20more"
print(unquote(encoded)) # hello world & more form_encoded = "search+term+with+spaces"
print(unquote_plus(form_encoded)) # search term with spaces

Python URL Encoding Gotchas

quote() vs quote_plus() — spaces matter

quote() encodes spaces as %20 (correct for URL paths). quote_plus() encodes spaces as + (correct for form data / query parameters). Using the wrong one causes subtle bugs.

quote() has a safe parameter

By default, quote() does not encode /. To encode everything, use quote(string, safe=''). The safe parameter specifies characters that should NOT be encoded.

Double encoding

Encoding an already-encoded string creates double-encoding: %20 becomes %2520. Always check if input is already encoded before calling quote(). Use unquote() first if unsure.

Python 2 vs Python 3

In Python 2, URL encoding functions were in urllib (urllib.quote, urllib.urlencode). In Python 3, they moved to urllib.parse. If you see ImportError, check your Python version.

Frequently Asked Questions

How do I URL-encode a string in Python?

Use urllib.parse.quote(string) for path components or urllib.parse.quote_plus(string) for query parameters. quote() encodes spaces as %20, while quote_plus() encodes spaces as +.

What is the difference between quote and quote_plus?

quote() encodes spaces as %20 and is suitable for URL path segments. quote_plus() encodes spaces as + and is designed for application/x-www-form-urlencoded query parameters.

How do I URL-decode a string in Python?

Use urllib.parse.unquote(string) to decode percent-encoded characters, or urllib.parse.unquote_plus(string) to also convert + back to spaces.

How do I encode query parameters from a dictionary?

Use urllib.parse.urlencode(dict) to convert a dictionary to a query string like key1=value1&key2=value2. For the requests library, just pass a params dict to requests.get().

Is this tool safe for encoding sensitive URLs?

Yes. All encoding and decoding happens entirely in your browser using client-side JavaScript. Your data is never sent to any server.