How to URL Encode a String
4 min read · Web & APIs
What is URL encoding?
URLs can only contain a limited set of characters. When a URL needs to include characters outside that set — like spaces, special characters, or non-ASCII text — those characters must be percent-encoded.
Percent encoding replaces each unsafe character with a %followed by two hexadecimal digits representing the character's byte value.
Hello World → Hello%20World café → caf%C3%A9 user@email → user%40email
Which characters are safe?
The following characters are safe in URLs and never need encoding:
A-Z a-z 0-9 - _ . ~
All other characters — including spaces, &, =, ?, /, and any Unicode characters — must be encoded when used as data (not as URL structure).
Common encoded characters
| Character | Encoded | Note |
|---|---|---|
| %20 | Space — very common | |
| ! | %21 | |
| # | %23 | Fragment identifier |
| $ | %24 | |
| & | %26 | Query separator |
| ' | %27 | |
| ( | %28 | |
| ) | %29 | |
| * | %2A | |
| + | %2B | Also means space in query strings |
| , | %2C | |
| / | %2F | Path separator |
| : | %3A | Scheme separator |
| ; | %3B | |
| = | %3D | Key-value separator |
| ? | %3F | Query string start |
| @ | %40 | |
| [ | %5B | |
| ] | %5D |
Encoding in JavaScript
JavaScript has two functions for URL encoding — use the right one:
encodeURIComponent() — encode a value inside a URL
const query = encodeURIComponent('hello world & more');
// "hello%20world%20%26%20more"
const url = `https://example.com/search?q=${query}`;
// "https://example.com/search?q=hello%20world%20%26%20more"Use this for query parameter values, path segments, or any user-supplied data placed inside a URL. It encodes everything except A–Z a–z 0–9 - _ . ! ~ * ' ( ).
encodeURI() — encode a full URL
const url = encodeURI('https://example.com/path with spaces');
// "https://example.com/path%20with%20spaces"
// Does NOT encode: : / ? # [ ] @ ! $ & ' ( ) * + , ; =Use this only to encode a complete URL. It preserves structural characters like /, ?, and & that are part of the URL structure.
Decoding
decodeURIComponent('hello%20world%20%26%20more');
// "hello world & more"
decodeURI('https://example.com/path%20with%20spaces');
// "https://example.com/path with spaces"Encoding in Python
from urllib.parse import quote, quote_plus, unquote
# Encode a path segment
quote('hello world') # "hello%20world"
quote('café') # "caf%C3%A9"
# Encode a query parameter value (space becomes +)
quote_plus('hello world') # "hello+world"
quote_plus('a=1 & b=2') # "a%3D1+%26+b%3D2"
# Decode
unquote('hello%20world') # "hello world"
unquote('caf%C3%A9') # "café"quote_plus encodes spaces as + instead of %20, which is the standard for HTML form submissions (application/x-www-form-urlencoded).
Space: %20 or +?
Both %20 and + represent a space, but in different contexts:
- %20 — correct in URL paths and in modern query strings (RFC 3986)
- + — only valid in HTML form data (
application/x-www-form-urlencoded)
When in doubt, use %20. It is always correct. A + in a URL path is a literal plus sign, not a space.
Common mistakes
- Double-encoding — encoding an already-encoded value turns
%20into%2520. Always decode before re-encoding. - Using encodeURI for query values — it does not encode
&and=, which will break your query string. - Forgetting to encode non-ASCII characters — accented letters, emoji, and CJK characters must all be encoded.
- Treating + as %20 in paths — outside of form data, a + in a path is a literal plus sign.
Encode and decode URLs online
Paste any string to encode or decode it instantly — no installation needed.
URL Encoder / Decoder →