URL Encoding in Python: urllib, requests, and Quick Tools
- Python encodes URLs with urllib.parse.quote() for individual values and urllib.parse.urlencode() for full query strings.
- The requests library handles encoding automatically — just pass a params dict and it builds the query string for you.
- For quick one-off encoding without Python, use the free Mongoose URL Encoder.
Table of Contents
Python's standard library has everything you need to URL encode strings — no third-party packages required. The two main functions are urllib.parse.quote() for individual values and urllib.parse.urlencode() for building complete query strings from a dictionary.
If you're already using the requests library for HTTP calls, you don't need to encode manually at all — pass a params dict and it handles encoding for you.
Encoding a Single String with urllib.parse.quote
urllib.parse.quote() encodes a single string, replacing special characters with percent-codes. By default it leaves the slash (/) unencoded:
from urllib.parse import quote
quote('hello world') # 'hello%20world'
quote('price=10&tax=2') # 'price%3D10%26tax%3D2'
quote('/path/to/file.pdf') # '/path/to/file.pdf' (slash preserved)
quote('/path/to/file.pdf', safe='') # '%2Fpath%2Fto%2Ffile.pdf'
The safe parameter controls which characters are not encoded. The default is safe='/'. Set safe='' to encode everything including slashes — useful when a path value will itself be a query parameter.
Building a Full Query String with urllib.parse.urlencode
urllib.parse.urlencode() takes a dict (or list of tuples) and returns a properly encoded query string:
from urllib.parse import urlencode
params = {
'q': 'coffee shops & cafes',
'city': 'New York',
'sort': 'rating'
}
urlencode(params)
# 'q=coffee+shops+%26+cafes&city=New+York&sort=rating'
By default it encodes spaces as + (form encoding). To get %20 instead, use quote_via=quote:
from urllib.parse import urlencode, quote
urlencode(params, quote_via=quote)
# 'q=coffee%20shops%20%26%20cafes&city=New%20York&sort=rating'
Sell Custom Apparel — We Handle Printing & Free Shipping
Letting the requests Library Handle Encoding
When making HTTP requests with the requests library, pass query parameters as a dict and encoding happens automatically:
import requests
response = requests.get(
'https://api.example.com/search',
params={
'q': 'coffee shops & cafes',
'city': 'New York'
}
)
# Actual URL: https://api.example.com/search?q=coffee+shops+%26+cafes&city=New+York
print(response.url) # Shows the encoded URL that was actually sent
This is the cleanest approach for API calls — no manual encoding, no formatting errors, and response.url shows you the exact encoded URL that was sent.
Decoding Percent-Encoded Strings in Python
Use urllib.parse.unquote() to decode a percent-encoded string:
from urllib.parse import unquote, unquote_plus
unquote('hello%20world') # 'hello world'
unquote('price%3D10%26tax%3D2') # 'price=10&tax=2'
unquote_plus('hello+world') # 'hello world' (decodes + as space)
Use unquote_plus() when decoding form-encoded data where spaces were encoded as +. Use unquote() for standard RFC 3986 percent-encoding where spaces are %20.
For quick decoding without Python, paste the encoded string into the Mongoose URL Encoder and click Decode.
URL Encode Without Python — Free Tool
No time to write code? Paste your string into the Mongoose URL Encoder and get the result instantly.
Open URL EncoderFrequently Asked Questions
What is the difference between quote and quote_plus in Python?
quote() encodes spaces as %20 (standard percent-encoding). quote_plus() encodes spaces as + and is designed for HTML form data (application/x-www-form-urlencoded). Use quote() for URL path and query values, quote_plus() for form payloads.
How do I URL encode a dictionary as query parameters in Python?
Use urllib.parse.urlencode(your_dict) to get a properly encoded query string, then append it to your URL: base_url + "?" + urlencode(params). Or just pass params=your_dict to requests.get() and let the library handle it.
Does requests handle URL encoding automatically?
Yes. When you pass a params argument to requests.get() or any other method, the library encodes the values for you and appends the query string to the URL. You can inspect the final URL via response.url.
How do I encode non-ASCII characters like accented letters in Python?
urllib.parse.quote() handles this natively. It converts non-ASCII characters to UTF-8 bytes first, then percent-encodes each byte. For example, quote("café") returns "caf%C3%A9".

