From the Python 3 documentation:

Copyurllib.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:

Copy>>> 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:

Copy>>> 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
For example, if a parameter value legitimately contains an equals sign, it should be encoded as =. 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).
Discussions

urllib - How to urlencode a querystring in Python? - Stack Overflow
That's really helpful! In my case, I only have a portion of string that I want to URL-encode, for example I want to transform my string to my string. Your solution works like a charm for that! 2016-02-14T11:27:02.76Z+00:00 ... In Python 3. More on stackoverflow.com
🌐 stackoverflow.com
How to url encode User Search Query Syntax for Management API in Python
I am trying to url encode this query string: email:john* and after quoting the string using Python urllib.parse.quote() my final URL looks like this: https://{domain}/api/v2/users?q=email:john* but the search doesn’t work because urllib.parse.quote() is also quoting the * which isn’t supposed ... More on community.auth0.com
🌐 community.auth0.com
1
0
January 26, 2024
Encode url including slashes?
urllib.parse.quote_plus is what you want. More on reddit.com
🌐 r/learnpython
5
0
October 1, 2022
URL encoding python
Posted by u/jabathehutstitty - 2 votes and 2 comments More on reddit.com
🌐 r/learnpython
2
2
October 2, 2020
🌐
Python
docs.python.org › 3 › library › urllib.parse.html
urllib.parse — Parse URLs into components
Use the urllib.parse.urlencode() function to convert such lists of pairs into query strings. Changed in version 3.2: Add encoding and errors parameters. Changed in version 3.8: Added max_num_fields parameter. Changed in version 3.10: Added separator parameter with the default value of &. Python versions earlier than Python 3.10 allowed using both ; and & as query parameter separator.
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › percent-encoding-url-encoding-with-python
Percent-encoding (URL Encoding) with Python | Binary Encoding Techniques Across Programming Languages
URLs often contain characters that aren't allowed or have special meanings, breaking web requests if not handled properly. Percent-encoding, or URL encoding, solves this by converting these characters into a format that's safe for transmission. This guide shows you how to perform percent-encoding reliably using Python's urllib.parse module.
🌐
URL Encoder
urlencoder.io › python
How to encode URLs in Python | URLEncoder
In Python 3+, You can URL encode any string using the quote() function provided by urllib.parse package.
Find elsewhere
🌐
Auth0
community.auth0.com › get help
How to url encode User Search Query Syntax for Management API in Python - Auth0 Community
January 26, 2024 - I am trying to url encode this query string: email:john* and after quoting the string using Python urllib.parse.quote() my final URL looks like this: https://{domain}/api/v2/users?q=email:john* but the search doe…
🌐
GitHub
gist.github.com › nkentaro › 37f25b802e825da7ab3b7f27c0303047
url encode and decode in python3 · GitHub
The quote() function encodes space to . Python also has a quote_plus() function that encodes space to plus sign (+). I've written about URL Encoding in python on my website.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-urlencode-a-querystring-in-python
How to Urlencode a Querystring in Python? - GeeksforGeeks
July 23, 2025 - This method handles encoding spaces as plus signs and ensures the parameters are properly formatted. ... import urllib.parse data = { "site": "GeeksforGeeks", "topic": "Python URL encoding", "level": "Intermediate" } encoded_data = urllib.parse.urlencode(data) print(encoded_data)
🌐
Temboo
temboo.com › python › url-encode
How to Encode a URL in Python
3Now let's test the Utilities > Encoding > URLEncode Choreo directly from our website. 4Select Python from the drop down menu at the top of the page, then enter some text that you want to URL encode. It should contain illegal characters, like spaces, accents, and punctuation.
🌐
Interzoid
interzoid.com › apis › json-url-encoding
API URL Encoding: Learn how to URL-encode JSON for use within an HTTP Query
Copy import json import urllib.parse def url_encode_json(data): # Convert the Python object to a JSON string json_string = json.dumps(data) # URL encode the JSON string encoded_string = urllib.parse.quote(json_string) return encoded_string # Your JSON data data = [ { "Data": "IBM" }, { "Data": "International Business Machines" }, { "Data": "ibm corp" } ] # Encode the data encoded_result = url_encode_json(data) print(encoded_result)
🌐
ScrapeOps
scrapeops.io › url encoding
URL Encoding | ScrapeOps
Here are some options of how to encode a URL in Python using urllib.parse.quote:
🌐
Python
docs.python.org › 3 › library › index.html
The Python Standard Library — Python 3.14.6 documentation
json — JSON encoder and decoder · mailbox — Manipulate mailboxes in various formats · mimetypes — Map filenames to MIME types · base64 — Base16, Base32, Base64, Base85 Data Encodings · binascii — Convert between binary and ASCII · quopri — Encode and decode MIME quoted-printable data ·
🌐
FFmpeg
ffmpeg.org
FFmpeg
Currently, the only codecs supported are: FFv1 (encoding and decoding) and ProRes RAW (decode only). ProRes (encode+decode) and VC-2 (encode+decode) implementations are complete and currently in review, to be merged soon and available with the next minor release.
🌐
iO Flood
ioflood.com › blog › python-url-encode
[SOLVED] Python URL Encode Using urllib.parse Functions
February 1, 2024 - We then define a URL with some special characters. When we pass this URL to the quote() function, it returns a new string where all special characters have been replaced with their corresponding percent-encoded ASCII characters. But there’s much more to Python URL encoding than just this.
🌐
Reddit
reddit.com › r/learnpython › url encoding python
r/learnpython on Reddit: URL encoding python
October 2, 2020 - >>> urllib.parse.quote(url) 'https://www.google.com/' >>> urllib.parse.quote(url, safe='') 'https://www.google.com/' https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote
🌐
Blogger
umar-yusuf.blogspot.com › 2020 › 05 › python-url-encode.html
Geospatial Solutions Expert: Python URL Encode
October 1, 2020 - quote_plus() and urlencode(). They slight do different encoding, however urlencode() is more frequently used so I will talk about it in few seconds. To encode the query string query_string = 'Hellö Wörld@Python' to our base url, the code will look like this:-
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.6 documentation
Passing a bytes object to str() without the encoding or errors arguments falls under the first case of returning the informal string representation (see also the -b command-line option to Python).
🌐
URL Encode
urlencoder.org
URL Encode and Decode - Online
Please wait until the encoding process is complete. ... Please note that this file is removed from our system immediately after the first download attempt or 15 minutes of inactivity. ... Need this tool again? Bookmark this page for instant access next time. Meet URL Decode and Encode, a simple online tool that does exactly what it says: decodes from URL encoding as well as encodes into it quickly and easily.