[Python] How to URL Encode and Decode Strings (Handling Non-ASCII Characters)

As a general rule, URLs in web browsers can only contain alphanumeric characters and certain symbols (ASCII characters). Therefore, if you want to include spaces, special symbols, or non-ASCII characters in a URL, you must convert them into a format called “Percent-Encoding” (e.g., %20 for a space).

In Python, you can easily perform this conversion (encoding) and restoration (decoding) using the standard library module urllib.parse.

目次

Implementation Example 1: Encoding and Decoding Strings

In this example, we will convert a string containing spaces into a format usable in URLs, and then restore it to the original string.

Source Code

from urllib import parse

# 1. URL Encode (quote)
# Example keyword: "Tourist Spot Tokyo" (contains spaces)
search_keyword = "Tourist Spot Tokyo"

# Convert to percent-encoding format using parse.quote(string)
# Spaces are converted to '%20'
encoded_text = parse.quote(search_keyword)

print(f"Original: {search_keyword}")
print(f"Encoded : {encoded_text}")

print("-" * 30)

# 2. URL Decode (unquote)
# Restore the encoded string back to the original format
decoded_text = parse.unquote(encoded_text)

print(f"Decoded : {decoded_text}")

Execution Result

Original: Tourist Spot Tokyo
Encoded : Tourist%20Spot%20Tokyo
------------------------------
Decoded : Tourist Spot Tokyo

Implementation Example 2: Creating Query Parameters from a Dictionary (urlencode)

In practical work, you often need to create multiple parameters (in the format ?key=value&id=123) rather than just a single string. In such cases, using the parse.urlencode() function is very convenient as it converts a dictionary directly into a query string.

Source Code

from urllib import parse

# Search condition parameters (Dictionary)
query_params = {
    "q": "Delicious Ramen",  # Search word (contains a space)
    "page": 1,
    "sort": "date",
    "region": "Shibuya"
}

# Convert dictionary to URL query string
# Automatically joins with '&' and encodes values
query_string = parse.urlencode(query_params)

# Combine with the base URL
full_url = "https://example.com/search?" + query_string

print("--- Generating Query Parameters ---")
print(full_url)

Execution Result

--- Generating Query Parameters ---
https://example.com/search?q=Delicious+Ramen&page=1&sort=date&region=Shibuya

Explanation

parse.quote(string, safe=’/’)

This function URL-encodes a string. By default, forward slashes / are not converted (to keep them as path separators). If you want to convert slashes to %2F as well, specify safe='' as an argument.

parse.urlencode(dict)

This function accepts a dictionary (or a list of two-element tuples) and generates a string in the format key1=value1&key2=value2. This is extremely useful when creating requests for Web APIs.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次