When working with URLs in Python, you may encounter percent-encoded characters like %20 or %2F. These encoded characters allow URLs to contain special characters like spaces that would otherwise break the URL syntax. To handle these properly, we need to understand URL encoding and decoding.
URL encoding converts characters into a percent-encoded format. For example, a space character is encoded as
In Python, the
from urllib.parse import quote, unquote
encoded = quote(some_string) # Encode the string
decoded = unquote(encoded) # Decode the encoded string
Here's a practical example:
from urllib.parse import quote, unquote
text = "Hello world"
encoded = quote(text)
# encoded is now "Hello%20world"
decoded = unquote(encoded)
# decoded is back to the original "Hello world"
As you can see,
The main reason you'd use this in Python is building URLs programmatically. For example:
from urllib.parse import urlencode
query_args = { "name": "John Doe" }
encoded_args = urlencode(query_args)
# Returns "name=John+Doe" with spaces encoded
So in summary, URL encoding/decoding allows handling spaces and special characters in URLs. Python's