The urllib
library comes preinstalled in Python so there is no need to install it:
import urllib.parse
def url_encode(url):
"""Encode a URL using percent-encoding"""
return urllib.parse.quote(url)
def url_decode(url):
"""Decode a URL using percent-encoding"""
return urllib.parse.unquote(url)
# Example usage
url = "https://www.example.com/?q=python programming"
encoded_url = url_encode(url)
print("Encoded URL:", encoded_url)
decoded_url = url_decode(encoded_url)
print("Decoded URL:", decoded_url)