Categories
Python Answers

How to percent-encode URL parameters in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *