URL Encoding Explained: Why Spaces Become %20
You paste a link into a chat or browser and notice that spaces have turned into %20. Other characters, such as ampersands and question marks, may also look different. Nothing is necessarily broken. This is URL encoding at work.
URL encoding helps represent text safely inside web addresses. It is especially important when a URL contains spaces, special characters, or non-English text.
This guide explains how URL encoding works, why spaces become %20, and how to encode and decode URL values using JavaScript and Python. You will also learn the difference between %20 and +, how special characters behave, and which common mistakes to avoid.
What Is URL Encoding?
URL encoding, also known as percent encoding, is a method of representing certain characters using a percent sign followed by two hexadecimal digits.
A URL has a defined structure, and some characters have special roles within it. Other characters need to be represented in a form that can be transmitted and interpreted consistently.
Percent encoding represents the relevant character bytes using a pattern like this:
%XX
Here, XX represents a two-digit hexadecimal value.
For example:
- A space becomes
%20. - An ampersand (
&) becomes%26when used as data. - An equals sign (
=) becomes%3Dwhen used as data. - A question mark (
?) becomes%3Fwhen used as data.
This method makes it possible to include characters in URL components without confusing them with the URL’s structural separators.
Percent Encoding
Percent encoding works with bytes, not directly with every character as a human sees it.
Each encoded byte is represented by a percent sign followed by its hexadecimal value.
For example, a space has the decimal byte value 32. Its hexadecimal representation is 20, so it becomes %20.
Similarly:
| Character | Decimal byte value | Percent-encoded form |
|---|---|---|
| Space | 32 | %20 |
& | 38 | %26 |
= | 61 | %3D |
? | 63 | %3F |
/ | 47 | %2F |
For characters outside basic ASCII, the character is first encoded into bytes, commonly using UTF-8. Each byte is then represented by its own percent-encoded value.
This is why a single visible character can sometimes produce multiple percent codes.
Why Spaces Become %20 in URLs
A raw space is not part of the standard URL character set used for URL syntax. It can also create ambiguity when URLs are copied, displayed, or processed by software.
For example, consider this text:
https://example.com/my travel guide
The space makes the address unsuitable as a conventional URL representation. Encoding the space produces:
https://example.com/my%20travel%20guide
The %20 sequences represent the original spaces. Software that processes the relevant URL component can decode them when needed.
This makes URLs easier to transmit and interpret consistently across browsers, servers, and applications.
Space Encoding in URLs: %20 vs. Plus
You may also see a plus sign (+) used to represent a space in URLs.
However, %20 and + are not interchangeable in every URL context.
The plus sign represents a space in the application/x-www-form-urlencoded format, which is commonly used when submitting HTML form data.
For example:
q=fish+%26+chips
In form-encoded data, this represents the value fish & chips.
In a normal URL path, however, a plus sign is generally treated as a literal plus sign rather than a space.
For example:
https://example.com/C++
Here, the plus signs are part of the path text.
Use %20 when you need to represent a space in a URL component without relying on form-encoding rules. Use + when working with form-encoded query data and the relevant parser expects that convention.
URL Encoding Special Characters
Some URL characters are called reserved characters because they have special meanings in URL syntax.
Common examples include:
| Character | Common role in a URL |
|---|---|
? | Introduces the query component |
& | Separates query parameters in common query-string formats |
= | Separates parameter names and values |
# | Introduces the fragment |
/ | Separates path segments |
: | Separates the scheme from other URL components |
These characters do not always need encoding. When they are being used for their structural purpose, they should generally remain as separators.
However, when one of them is part of a data value, it may need percent encoding.
Consider this search phrase:
fish & chips
If you want it to be the value of a query parameter, you can encode it like this:
https://example.com/search?q=fish%20%26%20chips
The ampersand inside the search phrase becomes %26, so it is not mistaken for a separator between two query parameters.
The characters that do not need percent encoding under the standard unreserved-character rules are:
- Letters from
AtoZandatoz. - Digits from
0to9. - Hyphen (
-). - Period (
.). - Underscore (
_). - Tilde (
~).
These are called unreserved characters.
URL Encoding Examples
Here are some common characters and their percent-encoded forms:
| Character or text | Encoded form |
|---|---|
| Space | %20 |
& | %26 |
= | %3D |
? | %3F |
# | %23 |
/ | %2F |
+ | %2B |
@ | %40 |
é | %C3%A9 |
hello world | hello%20world |
fish & chips | fish%20%26%20chips |
Notice the entry for é. It is not a basic ASCII character. In UTF-8, it is represented by two bytes:
C3 A9
Each byte becomes a separate percent-encoded sequence:
%C3%A9
This is why encoding non-English text can produce several percent codes for a single visible character.
How to Encode a URL
You usually do not need to encode URL values manually. Most programming languages provide built-in functions for this purpose.
The important thing is to choose a function that matches the part of the URL you are encoding.
URL Encoding in JavaScript
JavaScript provides two commonly used functions:
encodeURIComponent()for individual URL components, such as query parameter values.encodeURI()for a complete URL when you want to preserve its structural characters.
Using encodeURIComponent()
This function is usually the right choice when encoding user input or a query parameter value.
const search = "fish & chips"; const encoded = encodeURIComponent(search); console.log(encoded); // "fish%20%26%20chips"
It encodes characters such as spaces, ampersands, and question marks so they are treated as data rather than URL separators.
It does leave a small set of characters unescaped, including -, _, ., !, ~, *, ', (, ), so it should not be described as encoding every reserved character.
Using encodeURI()
This function is intended for a complete URI. It preserves characters that commonly form part of the URL structure.
const url = "https://example.com/my travel guide?q=books&sort=latest"; const encoded = encodeURI(url); console.log(encoded); // "https://example.com/my%20travel%20guide?q=books&sort=latest"
The spaces are encoded, while the URL’s structural characters, such as ?, &, and =, remain available.
Do not use encodeURI() to encode an individual query parameter value. Characters such as & and = may remain unescaped and interfere with the query structure.
URL Encoding in Python
Python provides URL encoding functions through the urllib.parse module.
Two useful functions are quote() and quote_plus().
Using quote()
The quote() function percent-encodes text. By default, it leaves / unescaped because it is often used for URL paths.
from urllib.parse import quote
print(quote("fish & chips"))
# fish%20%26%20chips
print(quote("my travel guide", safe=""))
# my%20travel%20guide
Use safe="" when you want characters such as / to be encoded as part of a single value.
Using quote_plus()
The quote_plus() function follows the form-encoding convention, where spaces become plus signs.
from urllib.parse import quote_plus
print(quote_plus("fish & chips"))
# fish+%26+chips
This is useful when preparing data for form-style query strings.
The choice between quote() and quote_plus() depends on the format expected by the receiving application.
How to Decode a URL
Decoding reverses percent encoding. It converts encoded byte sequences back into their original characters using the appropriate character encoding.
For example:
fish%20%26%20chips
decodes to:
fish & chips
Decoding is useful when reading application logs, debugging redirects, inspecting tracking links, or processing incoming URL parameters.
URL Decoding in JavaScript
JavaScript provides decodeURIComponent() for decoding individual URL components.
const decoded = decodeURIComponent("fish%20%26%20chips");
console.log(decoded);
// "fish & chips"
If you are decoding an entire URI rather than an individual component, JavaScript also provides decodeURI().
Use the matching function for the type of data you are decoding.
URL Decoding in Python
Python’s urllib.parse module provides unquote() for decoding percent-encoded values.
from urllib.parse import unquote
decoded = unquote("fish%20%26%20chips")
print(decoded)
# fish & chips
For form-encoded data, use unquote_plus() when plus signs represent spaces.
from urllib.parse import unquote_plus
decoded = unquote_plus("fish+%26+chips")
print(decoded)
# fish & chips
Why You Should Avoid Double Decoding
Decode a value only as many times as its format requires.
Consider this example:
%2520
The first decoding produces:
%20
A second decoding changes it into a space.
That second step may corrupt data if the original value was intended to contain the literal characters %20.
Double encoding can also happen when already encoded values are encoded again. For example, %20 becomes %2520 because the percent sign itself becomes %25.
An online URL encoder or decoder can help with quick checks, but use the correct built-in functions in production code.
URL Encoding vs. HTML Encoding
URL encoding and HTML encoding solve different problems.
URL encoding represents characters within URL components using percent codes.
HTML encoding, also called HTML entity encoding, represents special characters in HTML text or attribute contexts using character references.
For example:
| Character | URL encoding | HTML encoding |
|---|---|---|
& | %26 | & |
< | %3C | < |
> | %3E | > |
" | %22 | " |
Suppose you want to include a URL containing an ampersand inside an HTML link.
The URL may contain:
https://example.com/search?q=fish%20%26%20chips&sort=latest
When placing that URL inside an HTML attribute, the ampersand used as a query separator should be escaped as & in the HTML source:
<a href="https://example.com/search?q=fish%20%26%20chips&sort=latest"> Search </a>
The browser interprets the HTML entity and uses the correct URL.
The general rule is to encode URL components for URL use, then apply the appropriate HTML escaping when placing the resulting URL into HTML. These operations are not interchangeable.
Common URL Encoding Mistakes to Avoid
Even when you use built-in functions, a few mistakes can cause unexpected results.
1. Encoding a URL Twice
Encoding an already encoded value can change its meaning.
For example:
%20
becomes:
%2520
This happens because the percent sign is encoded as %25. Avoid repeatedly encoding values without checking whether they have already been processed.
2. Using encodeURI() on a Parameter Value
encodeURI() preserves important URL separators, including ampersands and equals signs.
If you use it on a query value containing &, that character may be interpreted as a parameter separator.
Use encodeURIComponent() for individual values instead.
3. Using encodeURIComponent() on a Complete URL
This function encodes characters that are needed to preserve the structure of a complete URL.
For example, it can encode the colon and slashes in https://, which is not what you want when preparing a full address.
Use encodeURI() when appropriate for a complete URI, and use component encoding for individual values.
4. Mixing Plus Signs and %20
A plus sign represents a space in form-encoded data, not in every URL context.
Check whether the receiving system expects standard percent encoding or application/x-www-form-urlencoded behavior.
5. Ignoring UTF-8
Non-ASCII characters should be converted using the expected character encoding, commonly UTF-8, before percent encoding.
Using inconsistent encodings can produce incorrect results, especially when URLs contain accented letters, symbols, or non-English text.
6. Encoding URL Separators Instead of Values
Do not blindly encode every character in a URL.
The scheme, domain, path separators, and query delimiters have structural roles. Encode the data values that need it while preserving the structure of the URL.
Conclusion
URL encoding helps represent characters safely inside web addresses. It replaces relevant bytes with a percent sign and two hexadecimal digits. A space becomes %20 because its hexadecimal byte value is 20.
Use encodeURIComponent() in JavaScript and quote() in Python when encoding individual values. Use encodeURI() when working with a complete URI, and choose quote_plus() when preparing form-encoded data.
Remember that %20 and + have different meanings depending on the URL context. Avoid double encoding, handle non-ASCII text using UTF-8, and keep URL encoding separate from HTML encoding.
Once you understand these rules, it becomes much easier to create, read, and debug URLs without accidentally changing their meaning.
Frequently asked questions
What is URL encoding?
URL encoding, also called percent encoding, represents certain characters using a percent sign followed by two hexadecimal digits. It allows characters to be represented safely within URL components.
Why does a space become %20?
A space has the decimal byte value 32, which is 20 in hexadecimal. Percent encoding writes that byte as %20.
Is %20 the same as a plus sign?
Not in every context. %20 represents a space through percent encoding. A plus sign represents a space in application/x-www-form-urlencoded data, but it is generally a literal plus sign in a normal URL path.
Which characters need URL encoding?
Spaces, non-ASCII characters, and reserved characters used as data may need encoding. The unreserved characters—letters, digits, hyphens, periods, underscores, and tildes—can remain unchanged.
Do I need to encode a whole URL?
Usually not. Encode the relevant URL components, such as query values or individual path segments. Preserve the scheme, domain, and structural separators.
What is the difference between encodeURI() and encodeURIComponent()?
encodeURI() is intended for a complete URI and preserves many structural characters. encodeURIComponent() is intended for individual components, such as query parameter values, and encodes characters that could otherwise interfere with URL structure.
How do I encode a URL in Python?
Use urllib.parse.quote() to percent-encode a value. Use quote_plus() when you need form-encoding behavior, where spaces become plus signs.
How do I decode %20 in a URL?
Use decodeURIComponent() in JavaScript or unquote() in Python. Both can convert %20 back into a space when used with the appropriate input.
Why does an encoded URL contain %25?
%25 is the percent-encoded representation of the percent sign. If an already encoded value is encoded again, its existing percent signs may become %25, producing double encoding.
Is URL encoding the same as encryption?
No. URL encoding is a representation method, not a security method. Anyone can decode percent-encoded text. Never use URL encoding to hide passwords, tokens, or confidential information.