Ever pasted a URL with spaces or special characters and seen it turn into a mess of %20 and
%3A? That's URL encoding (formally called percent-encoding) β and
understanding it is essential for building robust web applications and APIs.
Why URL Encoding Exists
URLs can only contain a limited set of ASCII characters. Characters like spaces, &,
=, #, and non-ASCII characters (Γ©, ΓΌ, δΈ) would break the URL structure.
Percent-encoding converts unsafe characters to a %HH format where HH is the hexadecimal byte
value.
Common Encodings Reference
| Character | Encoded | Why |
|---|---|---|
| Space | %20 |
Spaces break URL parsing |
| & | %26 |
Separates query parameters |
| = | %3D |
Separates key=value pairs |
| # | %23 |
Marks fragment identifier |
| ? | %3F |
Starts query string |
| / | %2F |
Path separator |
| @ | %40 |
User info delimiter |
| + | %2B |
Often means space in forms |
JavaScript: encodeURIComponent vs encodeURI
// encodeURIComponent β encode EVERYTHING except A-Z, 0-9, - _ . ~
encodeURIComponent("hello world&lang=c#")
// β "hello%20world%26lang%3Dc%23"
// encodeURI β preserves URL structure characters (: / ? # etc.)
encodeURI("https://example.com/search?q=hello world")
// β "https://example.com/search?q=hello%20world"JavaScript
Use encodeURIComponent() for query parameter values.
Use encodeURI() only for complete URLs where you want to preserve the structure.
C# / .NET URL Encoding
// For query parameter values
var encoded = Uri.EscapeDataString("hello world&lang=c#");
// β "hello%20world%26lang%3Dc%23"
// For HttpClient query building
var query = $"?q={Uri.EscapeDataString(searchTerm)}&page={page}";C#
Python URL Encoding
from urllib.parse import quote, urlencode
# Single value
quote("hello world&lang=c#")
# β "hello%20world%26lang%3Dc%23"
# Dictionary of parameters
urlencode({"q": "hello world", "lang": "c#"})
# β "q=hello+world&lang=c%23"Python
Don't double-encode! If your URL already contains %20, encoding it
again produces %2520. Always check whether your data is already encoded before applying
encoding.
Try URL Encoder / Decoder
Paste any text or URL and encode/decode instantly. Swap button toggles direction.
Open URL Encoder β