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.

Quick answer: For one form-style field value, use Python 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.

Four language APIs for encoding the same URL query value
Python, Java, PHP, and C# query-value encoding compared side by side.

Compare the same query value

Input value: tea & cake +

LanguageEncode one query/form valueExampleEncoded value
Pythonurllib.parse.quote_plusquote_plus("tea & cake +")tea+%26+cake+%2B
JavaURLEncoder.encodeURLEncoder.encode("tea & cake +", StandardCharsets.UTF_8)tea+%26+cake+%2B
PHPurlencodeurlencode('tea & cake +')tea+%26+cake+%2B
C#WebUtility.UrlEncodeWebUtility.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.

Encoding “tea & cake +” as one query value
The query value tea & cake + becomes tea+%26+cake+%2B in form-style encoding.

Why these three characters matter

How spaces, ampersands, and literal plus signs are encoded
A space becomes +, an ampersand inside data becomes %26, and a literal plus becomes %2B.

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.

Choose whether to encode a value, query, or full URL
Distinguish one encoded value, its assembled query string, and the complete URL.
Keep URL delimiters intact and encode data values only
Keep URL structure such as ?, &, and = intact; encode only parameter data.

When a query encoder is the wrong choice

LanguageFor one path/data componentWhat changes
Pythonurllib.parse.quote(value, safe="")A space is %20; the empty safe set also encodes / when it is data inside one segment.
JavaUse 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.
PHPrawurlencode(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.

Choose form-style query encoding or path-component encoding
Compare form-query space as + with path-component space as %20.

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

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.

Watch on YouTube