How to decode a UTF-8 URL string in Python?

Sometimes, we want to decode a UTF-8 URL string in Python.

In this article, we’ll look at how to decode a UTF-8 URL string in Python.

How to decode a UTF-8 URL string in Python?

To decode a UTF-8 URL string in Python, we can use the unquote function from the urllib.parse module.

For instance, we write:

from urllib.parse import unquote

url = 'example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0'
decoded = unquote(url)
print(decoded)

to decode the url with unquote and assign the decoded URL string to decoded.

Therefore, decoded is 'example.com?title=правовая+защита'.

Conclusion

To decode a UTF-8 URL string in Python, we can use the unquote function from the urllib.parse module.