DevToolsForYou

URL Encoder and Decoder in Python — Code Examples

URL Encoder and Decoder in PythonUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

Python's urllib.parse module provides quote() for encoding path segments, quote_plus() for form data, and urlencode() for building query strings.

Python 3
from urllib.parse import quote, unquote, quote_plus, unquote_plus, urlencode

# Encode a single value (path-safe, encodes spaces as %20)
encoded = quote("hello world & foo=bar")
print(encoded)  # hello%20world%20%26%20foo%3Dbar

# Decode
decoded = unquote("hello%20world%20%26%20foo%3Dbar")
print(decoded)  # hello world & foo=bar

# Form encoding (spaces as +, for application/x-www-form-urlencoded)
form_encoded = quote_plus("hello world & foo=bar")
print(form_encoded)  # hello+world+%26+foo%3Dbar

form_decoded = unquote_plus("hello+world+%26+foo%3Dbar")
print(form_decoded)  # hello world & foo=bar

# Build a query string from a dict
qs = urlencode({"q": "hello world", "page": 1})
print(qs)  # q=hello+world&page=1
Notes & gotchas
  • quote() is for path segments; quote_plus() is for form data (encodes spaces as +).
  • quote() does not encode / by default — pass safe='' to encode slashes too.
  • urlencode() uses quote_plus() internally, so it produces + for spaces.
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages