How to URL-Encode a String in Python

2026-06-24 · Updated 2026-06-25 · 4 min read

How to URL-encode a string in Python

Figure 1: URL-encoding a string in Python

URL (percent) encoding replaces characters that are unsafe in a URL with a % followed by their byte value in hex. A space becomes %20, & becomes %26, and so on. Python handles all of this through the standard urllib.parse module, no installation required.

Try every example in the free Python Executor playground.


quote: encode a single value

Use quote for one piece of text, such as a path segment or a single query value.

python
Run
from urllib.parse import quote

print(quote("name=AdA & co/path?x=1"))
# name%3DAdA%20%26%20co/path%3Fx%3D1

Notice the / was not encoded. By default quote treats / as safe (handy for paths). To encode everything, pass safe="":

python
Run
from urllib.parse import quote

print(quote("name=AdA & co/path?x=1", safe=""))
# name%3DAdA%20%26%20co%2Fpath%3Fx%3D1
quote vs quote with safe empty in the Python online compiler

Figure 2: quote() with and without safe="" in the online compiler


quote vs quote_plus

The difference is how they treat spaces. quote turns a space into %20. quote_plus turns it into +, which is what HTML forms use in query strings.

python
Run
from urllib.parse import quote, quote_plus

print(quote("blue cheese"))       # blue%20cheese
print(quote_plus("blue cheese"))  # blue+cheese
Function Space becomes Best for
quote %20 path segments
quote_plus + query-string values

urlencode: build a whole query string

When you have a dictionary of parameters, urlencode encodes each key and value and joins them with &.

python
Run
from urllib.parse import urlencode

params = {"q": "blue cheese", "page": 2}
print(urlencode(params))
# q=blue+cheese&page=2

url = "https://example.com/search?" + urlencode(params)
print(url)
# https://example.com/search?q=blue+cheese&page=2

Decode percent-encoded text

unquote reverses quote; unquote_plus also turns + back into a space.

python
Run
from urllib.parse import unquote, unquote_plus

print(unquote("a%20b%26c%3Dd%2Fe"))   # a b&c=d/e
print(unquote_plus("blue+cheese"))    # blue cheese

Which function should I use?

Task Use
Encode one path segment quote
Encode one query value quote_plus
Encode a full set of params urlencode
Decode %20 style text unquote
Decode + as space unquote_plus

URL-encode online (no code)

For a quick one-off, paste your text into the KeyDecryptor URL Encoder and Decoder and get the result instantly in your browser.


Frequently Asked Questions

What is the difference between quote and quote_plus?

quote encodes a space as %20; quote_plus encodes it as +. Use quote_plus for query-string values and quote for path segments.

Why is / not being encoded?

quote treats / as a safe character by default so it does not break URL paths. Pass safe="" to force every character to be encoded.

How do I encode a whole dictionary of query parameters?

Use urlencode(params). It encodes each key and value and joins them with & into a ready-to-use query string.

Is URL encoding the same as Base64?

No. URL encoding only escapes unsafe characters as %XX; Base64 re-represents raw bytes using a 64-character alphabet. They solve different problems.

Do I need to install urllib?

No. urllib.parse is part of the Python standard library.