Python URL Encoder & Decoder
Encode and decode URLs for your Python projects. Test URL encoding instantly, then use the Python code examples with urllib.parse for your application. All processing happens in your browser.
URL Encoder & Decoder
Encode and decode URL components or full URIs. Fast, private, and free.
Quick Reference
Component mode uses encodeURIComponent / decodeURIComponent — encodes everything except A-Z a-z 0-9 - _ . ~ ! * ' ( ). Best for query parameter values and path segments.
Full URI mode uses encodeURI / decodeURI — preserves URL structure characters like : / ? # [ ] @ ! $ & ' ( ) * + , ; =. Best for encoding an entire URL while keeping its structure intact.
How to URL encode in Python
Python's urllib.parse module: from urllib.parse import quote, quote_plus, urlencode. For path segments: quote('hello world') returns 'hello%20world'. For query parameters: quote_plus('hello world') returns 'hello+world' (spaces become +). For full query strings: urlencode({'key': 'value with spaces', 'q': 'a&b'}) returns 'key=value+with+spaces&q=a%26b'. The safe parameter controls which characters are NOT encoded: quote('/path/to/file', safe='/') preserves slashes.
URL encoding with the requests library
Python's requests library handles URL encoding automatically: requests.get('https://api.example.com/search', params={'q': 'hello world', 'page': '1'}). This correctly encodes the URL as ...?q=hello+world&page=1. For manual URL construction: from urllib.parse import urljoin, urlparse; full_url = urljoin(base_url, path). Never concatenate URLs with string formatting — use params= in requests or urlencode() to prevent injection and encoding errors.
Common Python URL encoding pitfalls
Pitfall 1: Double encoding — if a string is already encoded, encoding again turns %20 into %2520. Check first: unquote(s) == s means it's not encoded. Pitfall 2: quote() vs quote_plus() — use quote() for URL paths (spaces become %20), quote_plus() for query parameters (spaces become +). Pitfall 3: Unicode URLs — Python 3 handles Unicode natively, but some APIs expect punycode for international domain names: domain.encode('idna'). Use this tool to verify your encoded output matches what your API expects.
Frequently Asked Questions
What is the difference between quote() and quote_plus() in Python?
quote() encodes spaces as %20, which is correct for URL paths. quote_plus() encodes spaces as +, which is correct for query string parameters (application/x-www-form-urlencoded). Use quote() for URL paths and quote_plus() for form data and query parameters.
How do I URL encode a dictionary in Python?
Use urllib.parse.urlencode(): from urllib.parse import urlencode; query = urlencode({'name': 'John Doe', 'city': 'New York'}). This returns 'name=John+Doe&city=New+York'. For nested data or lists, use urlencode(params, doseq=True).
How do I decode a URL-encoded string in Python?
Use urllib.parse.unquote() for %20-style encoding: unquote('hello%20world') returns 'hello world'. Use unquote_plus() for +-style encoding: unquote_plus('hello+world') returns 'hello world'. For full query strings: parse_qs('a=1&b=2') returns {'a': ['1'], 'b': ['2']}.