PercentURL / URL encoding in Python, Java, PHP, and C#
URL Encoding in Python, Java, PHP, and C#: One Query, Four APIs
Use the right function for a query value, keep URL separators intact, and see exactly how four common language APIs handle the same input.
quote_plus, Java URLEncoder.encode with UTF-8, PHP urlencode, or C# WebUtility.UrlEncode. All four encode a space as +, an ampersand inside the value as %26, and a literal plus as %2B. In Python, urlencode builds the complete query from keys and values. Do not apply these form/query encoders to a complete URL or path segment.
This comparison uses one input in all four languages to show how each form-style API handles the same characters. A path segment or an entire URL has different boundaries.

Compare the same query value
Input value: tea & cake +
| Language | Encode one query/form value | Example | Encoded value |
|---|---|---|---|
| Python | urllib.parse.quote_plus | quote_plus("tea & cake +") | tea+%26+cake+%2B |
| Java | URLEncoder.encode | URLEncoder.encode("tea & cake +", StandardCharsets.UTF_8) | tea+%26+cake+%2B |
| PHP | urlencode | urlencode('tea & cake +') | tea+%26+cake+%2B |
| C# | WebUtility.UrlEncode | WebUtility.UrlEncode("tea & cake +") | tea+%26+cake+%2B |
This table compares one encoded value at a time. Python's urlencode({"q": "tea & cake +"}) builds the full one-pair query and returns q=tea+%26+cake+%2B; it encodes the key/value pair and adds the query structure. The other three functions here encode only the value. Percent-escape hex letters are case-insensitive.

Why these three characters matter
- Space →
+: these APIs use form-style query serialization. Parsers for that format read a plus as a space. - Ampersand →
%26: an unescaped&separates query parameters. Encoding it inside the value keeps it as data. - Literal plus →
%2B: this prevents a plus that belongs to the input from being read as a space.

Build the query; do not encode the whole URL
A URL contains both data and structural characters. Keep ?, &, and = as URL syntax; encode each parameter name/value with the API that builds the query, then let a URL builder combine the parts.
from urllib.parse import urlencode
query = urlencode({"q": "tea & cake +"})
url = "https://example.com/search?" + query
# https://example.com/search?q=tea+%26+cake+%2B
Calling an encoder on https://example.com/search?q=... as one string would also encode its scheme and separators. If the value is already encoded, encoding it again changes %26 to %2526.


When a query encoder is the wrong choice
| Language | For one path/data component | What changes |
|---|---|---|
| Python | urllib.parse.quote(value, safe="") | A space is %20; the empty safe set also encodes / when it is data inside one segment. |
| Java | Use your HTTP framework's URI/query builder for the target component. | URLEncoder is for form data; use a builder that encodes individual components rather than applying it to a complete URI. |
| PHP | rawurlencode(value) | Follows RFC 3986 component rules; a space is %20. |
| C# | Uri.EscapeDataString(value) | Escapes a data component; it does not define form-style plus-as-space behavior. |
Choose based on where the data goes: a query/form value, one path segment, or a nested URL value. If your framework provides a query builder, use it to assemble the finished URL.

Common mistakes
Encoding the separators with the value
Do not encode a complete URL to protect one parameter. Encode the value first, then assemble the URL. This keeps real query separators separate from characters that are part of the value.
Using + as a space in every URL location
A plus represents a space in form-style query data. In a path, it can be a literal plus. Use the encoder and decoder for the component and format your application expects.
Encoding an already encoded value again
If % is encoded a second time, %26 becomes %2526. Start from the original value and encode once at the point where it enters the URL.
Official references
- Python:
urllib.parse—urlencode,quote, andquote_plus. - Oracle Java:
URLEncoder— form encoding, UTF-8, and space handling. - Oracle Java:
URI— URI parsing and construction. - PHP:
urlencodeandrawurlencode. - Microsoft:
WebUtility.UrlEncodeandUri.EscapeDataString. - RFC 3986 §2.1 — percent-encoding syntax.
Watch: URL Encoding in Python, Java, PHP & C#
Compare how the same query value is encoded across four languages, and see when to choose query or path-component encoding.