Introduction

URL encoding is a process of converting special characters and spaces in a string into a format that can be easily shared in a URL. However, sometimes we may need to reverse this process and decode a URL-encoded string back to its original form. In this tutorial, we will explore how to URL decode a string in Python.

What is URL Decoding?

URL decoding is the process of converting URL-encoded strings, where special characters are represented by their percent-encoded (%) values, back to their original form. For example, the URL-encoded string “"%20%2F”" represents a space followed by a forward slash.

URL Decoding in Python

Python provides a built-in module called urllib.parse that contains utility functions for parsing URLs. We can use the unquote function from this module to decode URL-encoded strings.

Here’s an example code snippet that demonstrates how to decode a URL-encoded string using the unquote function:

import urllib.parse

url_encoded_string = ""%20%2FHello%2C%20World%21""

decoded_string = urllib.parse.unquote(url_encoded_string)

print(decoded_string)

In the above code, we first import the urllib.parse module. Then, we define a variable url_encoded_string that contains the URL-encoded string we want to decode. We pass this string to the unquote function, which returns the decoded string. Finally, we print the decoded string.

When you run the above code, it will output:

 /Hello, World!

As you can see, the URL-encoded string %20%2FHello%2C%20World%21 is correctly decoded to /Hello, World!.

Handling Malformed URL-encoded Strings

It’s worth noting that the unquote function can also handle malformed URL-encoded strings. If an invalid percent-encoded sequence is encountered, the unquote function will replace it with the Unicode replacement character (U+FFFD) and continue decoding the rest of the string. This behavior helps prevent unexpected exceptions and allows graceful handling of malformed input.

Conclusion

URL decoding is a useful operation when working with URL-encoded strings in Python. By using the unquote function from the urllib.parse module, we can easily decode URL-encoded strings and obtain the original representation. Remember to import the urllib.parse module before using the unquote function.

I hope you found this tutorial helpful!