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
🌐
Python
docs.python.org › 3 › library › urllib.parse.html
urllib.parse — Parse URLs into components
Use the urllib.parse.urlencode() function (with the doseq parameter set to True) to convert such dictionaries into query strings. Changed in version 3.2: Add encoding and errors parameters.
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
The function you want is quote: >>> urllib.parse.quote(url) 'https%3A//www.google.com/' >>> urllib.parse.quote(url, safe='') 'https%3A%2F%2Fwww.google.com%2F' https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote More on reddit.com
🌐 r/learnpython
2
2
October 2, 2020
🌐
SSOJet
ssojet.com › escaping › url-escaping-in-python
URL Escaping in Python | Escaping Techniques in Programming
When building URLs dynamically in Python, particularly for web applications or API calls, encoding special characters is crucial. The urllib.parse.quote() function from Python's standard library is your primary tool for this.
🌐
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.
🌐
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…
Find elsewhere
🌐
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)
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › encode url including slashes?
r/learnpython on Reddit: Encode url including slashes?
October 1, 2022 -

Hello i would like to encode an url like this:
http://www.foodserv.co.za

I tried it with the following code

import urllib
link = urllib.parse.quote(link)
print(link)

But with that i only get: http%3A//www.foodserv.co.za

But i would like to have also the // encoded and in the end i should look like this: http%3A%2F%2Fwww.foodserv.co.za

Is there some other way or parameter to encode the url like i need it?

🌐
ScrapeOps
scrapeops.io › url encoding
URL Encoding | ScrapeOps
Here are some options of how to encode a URL in Python using urllib.parse.quote:
🌐
Reddit
reddit.com › r/learnpython › url encoding python
r/learnpython on Reddit: URL encoding python
October 2, 2020 -

hello I am trying to encode the special characters in a URL so that for example:

https://www.google.com/ would equal https%3A%2F%2Fwww.google.com%2F

I tried

url = "https://www.google.com/" 
url = url.encode("ascii")

but I got:

b'https://www.google.com/'

and I also tried urllib:

url = "https://www.google.com/"

url = urllib.parse.urlencode(url)

but got:

line 906, in urlencode
    raise TypeError

any help would be much appreciated thank you in advance!!!!!!!!!

🌐
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)
🌐
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.
🌐
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).