URL Encoding Explained: %20, Plus Signs, UTF-8 and the Double Encoding Bug
URL encoding, also called percent encoding, turns characters that are unsafe in a URL into a percent sign and two hex digits. Learn which characters need it, why a space can become %20 or a plus, how emoji become UTF-8 bytes, and which Python and JavaScript functions to use.
- URL encoding replaces a byte with
%plus two hex digits. A space is byte 0x20, so it becomes%20. - Non-ASCII text is first converted to UTF-8 bytes:
ébecomes%C3%A9and the coffee emoji becomes%E2%98%95. - A plus sign means a space only in form-style query strings. In a path,
+is a literal plus. - Encoding twice turns
%20into%2520. That double encoding bug is the most common URL encoding mistake.
You search for “QA & Dev” on a site and the results page shows only “QA”. You share a link to a file called “report 2026.pdf” and it 404s. You pass an email address with a plus sign to an API and the plus turns into a space. All three are URL encoding problems. A URL is only allowed to contain a small set of characters, and everything else must be encoded in a way both sides agree on.
This guide explains the rules and shows them with code. Every output below came from real runs with Python 3.13.6 and Node.js 24.22.1. For background on how text becomes bytes, see our UTF-8 encoding guide.
What is URL encoding?
URL encoding, formally called percent encoding in RFC 3986, represents a byte as a percent sign followed by its value in two hexadecimal digits. The space character has byte value 32, which is 20 in hex, so it becomes %20. An ampersand is 0x26, so it becomes %26. Decoding reverses the process.
The percent sign itself is the escape character, so a literal percent must also be encoded, as %25. That one rule is behind the double encoding bug later in this guide.
Why URLs need encoding
Some characters have a job inside a URL. Look at this one:
https://example.com/search?q=tea&page=2#top
The ? starts the query, & separates parameters, = splits names from values, # starts the fragment and / separates path segments. If a search term contains &, the server reads it as the start of a new parameter. That is exactly why “QA & Dev” became “QA” in the example above. Encoding the value turns & into %26, which the server treats as data. Our guide to HTTP methods shows where query strings fit into a request.
Which characters must be encoded?
| Group | Characters | Rule |
|---|---|---|
| Unreserved | A-Z a-z 0-9 - . _ ~ |
Never need encoding |
| Reserved | : / ? # [ ] @ ! $ & ' ( ) * + , ; = |
Encode when used as data inside a component |
| Everything else | Space, ", <, %, non-ASCII letters, emoji |
Always encode |
The key phrase is “used as data”. A slash that separates path segments must stay a slash, but a slash inside a single value, such as a file name, must become %2F. That is why you encode each piece of a URL separately rather than encoding a whole URL at the end.
Non-ASCII characters and UTF-8
Percent encoding works on bytes, not characters. Text outside ASCII is first converted to UTF-8, then each byte is encoded:
>>> "café ☕".encode("utf-8")
b'caf\xc3\xa9 \xe2\x98\x95'
>>> quote("café ☕")
'caf%C3%A9%20%E2%98%95'
The letter é takes two bytes, C3 A9, and the coffee emoji takes three, E2 98 95. Browsers often display the decoded characters in the address bar, but what travels over the network is the encoded form.
A space: %20 or plus?
Both are correct, in different places:
%20is the standard percent encoding and works everywhere in a URL.+means a space only in theapplication/x-www-form-urlencodedformat, used by HTML forms and most query strings.
The trap is decoding. Python’s two decoders show the difference clearly:
>>> unquote("a+b%20c"), unquote_plus("a+b%20c")
('a+b c', 'a b c')
If your data can contain a real plus sign, such as a phone number or an email alias like name+tag@example.com, it must be sent as %2B in a query string, or the server will read a space.
URL encoding in Python
The urllib.parse module has everything you need:
from urllib.parse import quote, quote_plus, unquote
s = "QA & Dev/Ops 100%"
print(quote(s))
print(quote(s, safe=""))
print(quote_plus(s))
QA%20%26%20Dev/Ops%20100%25
QA%20%26%20Dev%2FOps%20100%25
QA+%26+Dev%2FOps+100%25
Notice the slash. quote keeps / by default, because it was designed for paths. That is wrong for a single value such as a file name or an ID, so pass safe="" to encode it. quote_plus uses plus for spaces and encodes slashes, which suits form values. The requests library encodes query parameters for you when you pass a params dictionary, as our Python requests guide shows.
URL encoding in JavaScript
JavaScript has two built-in functions with very different jobs:
encodeURIComponent("QA & Dev/Ops 100%")
// 'QA%20%26%20Dev%2FOps%20100%25'
encodeURI("https://example.com/a b?x=1&y=café")
// 'https://example.com/a%20b?x=1&y=caf%C3%A9'
encodeURIComponent is for a single value: it encodes /, ?, & and =. encodeURI is for a whole URL: it leaves those structural characters alone, so it cannot protect a value that contains &. In practice, use encodeURIComponent for every value you insert into a URL. Both leave ! ' ( ) * ~ - . _ unencoded. Avoid the old escape() function; it produced caf%E9 for “café”, a Latin-1 encoding that modern servers will not decode as UTF-8.
Building query strings safely
Better than encoding by hand is letting a library build the whole query string:
# Python
urlencode({"q": "a+b c", "tag": ["x", "y"]}, doseq=True)
# 'q=a%2Bb+c&tag=x&tag=y'
// JavaScript
const u = new URL("https://example.com/search");
u.searchParams.set("q", "tea & ☕");
u.href
// 'https://example.com/search?q=tea+%26+%E2%98%95'
Both encoded the real plus as %2B and the space as +, so the round trip is safe. When reading, use the matching parser: Python’s parse_qs("q=a+b%2Bc") returned {'q': ['a b+c']}, and JavaScript’s new URLSearchParams("q=a+b%2Bc").get("q") returned 'a b+c'. The same advice applies to the fetch API: build the URL with URL and searchParams rather than string concatenation.
Tip: never build URLs by joining strings with user input. Use URLSearchParams, urlencode or your HTTP client’s params option, and encode each path segment on its own.
The double encoding bug
If you encode a value that is already encoded, the percent signs get encoded too:
once = quote("report 2026.pdf") # 'report%202026.pdf'
twice = quote(once) # 'report%25202026.pdf'
unquote(twice) # 'report%202026.pdf'
%20 became %2520, because % was encoded as %25. After one decode, the server sees the literal text report%202026.pdf and returns a 404. This usually happens when one layer of code encodes a value and another layer, such as an HTTP client, encodes it again. The rule is simple: encode exactly once, at the point where you build the URL, and pass raw values everywhere else. If you see %25 followed by two hex digits in your logs, you almost certainly have double encoding.
What happens with broken input
Malformed percent sequences behave differently by language. We decoded %E2%98, which is an incomplete emoji, and %zz, which is not hex at all:
# Python
unquote("%E2%98") # '�' (replacement character)
unquote("%zz") # '%zz' (left as is)
unquote("%E2%98", errors="strict")
# UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: unexpected end of data
// JavaScript
decodeURIComponent("%E2%98")
// URIError: URI malformed
Python quietly replaces bad bytes with the replacement character, while JavaScript throws. Neither is wrong, but your code must handle it: wrap decodeURIComponent in try and catch for any value that comes from users. JavaScript also threw URIError when we tried to encode a lone surrogate character, which can appear when a string is cut in the middle of an emoji.
Encoding is not security
Percent encoding makes data travel safely through a URL. It does not make the data safe to use. An attacker can encode ../ as %2E%2E%2F to try path traversal, or encode a script tag to get past a naive filter. Always decode first, then validate, then use the value. Escape it again for the place it lands: HTML escaping for pages and parameterised queries for SQL. Encoding is also not encryption: Base64 and percent encoding are both reversible by anyone.
Testing tips
- Test inputs with a space,
&,+,%,/,#, an accented letter and an emoji. Most encoding bugs show up with that one list. - Check the round trip: what you send should equal what the server stores and returns.
- Search API logs for
%25to catch double encoding. - Send malformed sequences such as
%zzand%E2and check the API returns a clear 400 rather than a 500. Our what is an API guide covers status codes in context.
Cheat sheet
| Character | Encoded | Character | Encoded |
|---|---|---|---|
| space | %20 or + |
& |
%26 |
+ |
%2B |
= |
%3D |
/ |
%2F |
? |
%3F |
# |
%23 |
% |
%25 |
: |
%3A |
@ |
%40 |
| é | %C3%A9 |
☕ | %E2%98%95 |
Final thoughts
URL encoding comes down to a few rules: encode each value once, at the moment you build the URL; use component-level functions such as encodeURIComponent or quote(value, safe=""); remember that plus means space only in form-style query strings; and treat decoded input as untrusted. Follow those and spaces, ampersands, plus signs and emoji will all survive the trip intact.
Frequently asked questions
What does %20 mean in a URL?
It is an encoded space. The space character has byte value 32, which is 20 in hexadecimal, and percent encoding writes a byte as a percent sign followed by two hex digits.
Should a space be encoded as %20 or a plus sign?
Use %20 anywhere in a URL. A plus sign means a space only in form-style query strings, so a path or a value that may contain a real plus should use %20 and %2B.
What is the difference between encodeURI and encodeURIComponent?
encodeURI is for a whole URL and leaves characters such as slash, question mark and ampersand alone. encodeURIComponent is for a single value and encodes them, so use it for anything you insert into a URL.
Why does %2520 appear in my URL?
The value was encoded twice. The first pass turned a space into %20, and the second pass encoded the percent sign as %25. Encode only once, where the URL is built.
Is URL encoding the same as encryption?
No. It only makes characters safe to place in a URL and anyone can decode it. Always validate decoded values and use HTTPS to protect data in transit.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.