From the Python 3 documentation:

urllib.parse.quote(string, safe='/', encoding=None, errors=None)

Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-~' are never quoted. By default, this function is intended for quoting the path section of a URL. The optional safe parameter specifies additional ASCII characters that should not be quoted — its default value is '/'.

That means passing '' for safe will solve your first issue:

>>> import urllib.parse
>>> urllib.parse.quote('/test')
'/test'
>>> urllib.parse.quote('/test', safe='')
'%2Ftest'

(The function quote was moved from urllib to urllib.parse in Python 3.)

By the way, have a look at urlencode.


About the second issue, there was a bug report about it and it was fixed in Python 3.

For Python 2, you can work around it by encoding as UTF-8 like this:

>>> query = urllib.quote(u"Müller".encode('utf8'))
>>> print urllib.unquote(query).decode('utf8')
Müller
Answer from Nadia Alramli on Stack Overflow
🌐
SSOJet
ssojet.com › escaping › url-escaping-in-python
URL Escaping in Python | Escaping Techniques in Programming
Always escape characters that aren't alphanumeric or part of the unreserved set (-, ., _, ~) when constructing URLs dynamically. Python's urllib.parse module is indispensable for handling Uniform Resource Locators (URLs).
🌐
MojoAuth
mojoauth.com › escaping › url-escaping-in-python
URL Escaping in Python | Escaping Methods in Programming Languages
In the context of URL escaping, specific characters such as spaces, punctuation marks, and reserved characters require encoding. The most commonly used special characters include: ... Exclamation mark: Encoded as ! ... In Python, the urllib.parse module provides functions to perform URL encoding and decoding.
🌐
Hristog
hristog.github.io › unquoting_url_escape_characters_using_python.html
Unquoting URL escape characters using Python — Conscious Automaton
November 5, 2017 - In [4]: url = urllib2.unquote(url).decode('utf8') In [5]: url Out[5]: u'https://www.a-random-ml-website.com/na\xefve-bayes' In [6]: import unicodedata In [7]: better_looking_url = unicodedata.normalize('NFKD', url).encode('ascii', 'ignore') In [8]: better_looking_url Out[8]: 'https://www.a-random-ml-website.com/naive-bayes'
🌐
Python
docs.python.org › 3 › library › urllib.parse.html
urllib.parse — Parse URLs into components — Python 3.14.6 ...
urllib.parse.unquote(string, encoding='utf-8', errors='replace')¶ · Replace %xx escapes with their single-character equivalent.
🌐
Quora
quora.com › How-do-I-escape-a-unicode-URL-in-Python
How to escape a unicode URL in Python - Quora
Answer: If you have a unicode string you can do this: [code python] from urllib2 import quote escaped_string = quote(unicode_string.encode('utf-8')) [/code] All those � are a telltale of UTF-8. Unicode strings in python are "raw" unicode, so make sure to .encode() and .decode() them as approp...
Find elsewhere
🌐
ScrapeOps
scrapeops.io › url encoding
URL Encoding | ScrapeOps
require 'httparty' require 'cgi' response = HTTParty.get( 'https://proxy.scrapeops.io/v1/', query: { 'api_key' => 'YOUR_API_KEY', 'url' => CGI.escape('https://example.com/?test=hello'), }, timeout: 120, ) puts 'Body: ' + response.body
🌐
Medium
medium.com › @ericvanrees › python-workout-url-encode-specicharacters-bba47e03540d
Python Workout — URL-encode special characters | by Eric van Rees | Medium
September 28, 2022 - This is explained in this overview of Python’s built-in functions. So, the URL-encoding operation is done using the ord() and hex() functions, but the main challenge for this exercise was to find a way to filter a string for letters and numbers. Turns out there are two functions that do exactly this: .isalpha() and .isdigit(). Using these in an if/else statement, you can easily filter a string with multiple special characters, such as “ABC999zz#~€&/()=?¿”.
🌐
W3Schools
w3schools.com › tags › ref_urlencode.ASP
HTML URL Encoding Reference
Click the "URL Encode" button to see how the JavaScript function encodes the text. Note: The JavaScript function encodes space as . Your browser will encode input, according to the character-set used in your page.
🌐
iO Flood
ioflood.com › blog › python-url-encode
[SOLVED] Python URL Encode Using urllib.parse Functions
February 1, 2024 - To encode a URL in Python, you can use the quote() function from the urllib.parse module like encoded_url = quote(url). This function converts special characters in your URL into safe ASCII characters, making them safe for transport over the ...
🌐
GitHub
gist.github.com › Paradoxis › 6336c2eaea20a591dd36bb1f5e227da2
Agressive URL encode · GitHub
function url-encode() { python ~/<path to script>/url_encode.py $@ } Now simply call the script from your command line :) $ url-encode "foo" foo $ url-encode --agressive "foo" foo $ echo "foo" | url-encode foo $ echo "foo" | url-encode --agressive foo ... This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
URL Encoder
urlencoder.io › python
How to encode URLs in Python | URLEncoder
URL Encode online. URLEncoder is a simple and easy to use online tool to convert any string to URL Encoded format in real time. It also contains several articles on how to URL Encode a query string or form parameter in different programming languages.
🌐
pytz
pythonhosted.org › percentcoding
percentcoding - fast url encoding and decoding
This is not surprising; the standard implementations are pure Python. $ ./benchmark.py percentcodec.encode x 10000 0.348151922226 percentcodec.decode x 10000 0.381587028503 urllib.quote x 10000 4.51035284996 urllib.unquote x 10000 3.50923490524 ... All ASCII characters not occurring in the safe set are considered unsafe and will be escaped by encode.
🌐
DNMTechs
dnmtechs.com › decoding-escaped-characters-in-url-using-python-3
Decoding Escaped Characters in URL using Python 3 – DNMTechs – Sharing and Storing Technology Knowledge
Decoding escaped characters in URLs is a common task in web development and data processing. Many popular frameworks and libraries provide functions or methods to handle URL decoding. For example: The Django framework provides the urllib.parse.unquote() function for decoding URLs in Python.
🌐
Online Tools Forge
onlinetoolsforge.com › home › blog › url encoding guide: safe characters and special cases
URL Encoding Guide: Safe Characters and Special Cases - Online Tools Forge | Online Tools Forge
November 8, 2025 - // URL Encoding (for URLs) "hello world" → "hello world" // HTML Entity Encoding (for HTML) "hello world" → "hello world" (no change needed) "<script>" → "&lt;script&gt;" // JavaScript Encoding (for JavaScript) "hello world" → "hello world" (no change) "alert('hi')" → "alert(\'hi\')" (escape quotes) Step 1: Enter your text · [email protected] 2025 · Step 2: Choose operation · Encode (add % codes) Decode (remove % codes) Step 3: Get result · user@example.com 2025 · URL Encoder/Decoder Tool · JavaScript: // Encode encodeURIComponent('hello world!'); // Result: "hello world!" // Decode decodeURIComponent('hello world!'); // Result: "hello world!" Python: from urllib.parse import quote, unquote # Encode quote("hello world!") # Result: "hello world!"
🌐
Blogger
umar-yusuf.blogspot.com › 2020 › 05 › python-url-encode.html
Geospatial Solutions Expert: Python URL Encode
October 1, 2020 - Those space need to be converted or encoded into internet friendly character that will look like this "https://www.google.com/search?q=Advanced mobile success". Some regular modern browsers have the ability to do this encoding on the fly without you knowing. But when you want to construct such query url in a program or to communicate with an API, it is save if you encode them before parsing them. In python, this encoding can be handled using the urllib.parse module.
🌐
Testsigma
testsigma.com › home › free tools › url encoder
Free Online URL Encoder & Decoder Tool
When embedding into HTML contexts, remember to encode accordingly; sometimes you′ll need both HTML-entity encoding and URL encoding (e.g., when injecting into <a href> values). The key standard for URI encoding is RFC 3986 – it defines the reserved and unreserved characters and the mechanics of percent-encoding. ... In other languages like Python: urllib.parse.quote() or quote_plus().
🌐
Urlencoder
urlencoder.net
URL Encode and Decode - Online Tool
URIs that differ only by whether a reserved character is percent-encoded or appears literally are normally considered not equivalent unless it can be determined that the reserved characters in question have no reserved purpose. This determination is dependent upon the rules established for reserved characters by individual URI schemes. Source: Url encoding on Wikipedia · PHP urlencode() PHP rawurlencode() Python urlencode() C# UrlEncode() Ruby url_encode · Perl uri_escape ·