[Python] Percent-encode text with urllib.parse.quote

Created on November 30, 2026

You can use urllib.parse.quote() to escape characters that need percent encoding. In this example, the default settings preserve / and encode characters such as :, ?, and =.

from urllib.parse import quote

text = "https://www.example.com/?dummy=hello"
print(quote(text))
https%3A//www.example.com/%3Fdummy%3Dhello

Use quote(text, safe="") when slashes also need to be escaped, such as when encoding a single path segment. To build a query string from a mapping of names to values, use urllib.parse.urlencode().

Python