Menu Close

How to percent-encode URL parameters in Python?

Sometimes, we want to percent-encode URL parameters in Python.

In this article, we’ll look at how to percent-encode URL parameters in Python.

How to percent-encode URL parameters in Python?

To percent-encode URL parameters in Python, we can use the urllib.parse module.

For instance, we write

import urllib.parse

print(urllib.parse.quote("Müller".encode('utf8')))

to call urllib.parse.quote with the "Müller".encode('utf8')) UTF string to percent encode the string.

We can decode the string with unquote by writing

print(urllib.parse.unquote("M%C3%BCller"))

Conclusion

To percent-encode URL parameters in Python, we can use the urllib.parse module.

Posted in Python, Python Answers