Python 3.8’s New Query String Parsing Features

You heard that right, no longer do you have to spend hours manually decoding those ***** URL parameters like a caveman.
First things first, let’s take a look at what query string parsing is and why it’s so ***** important in the world of web development. Essentially, when you visit a website and enter a search term or filter some results, that information gets sent to the server as part of the URL. This data is then parsed by the server-side code to determine what action should be taken (e.g., displaying specific products based on category).
In previous versions of Python, query string parsing was a bit of a pain in the neck. You had to manually extract each parameter and its corresponding value using various methods like `split()` or `parse_qs()`. But with 3.8’s new feature, we can now use the built-in `urlparse()` function to handle all that for us!
Here’s an example:

# Import the urllib.parse module to access the built-in function for parsing URLs
import urllib.parse

# Create a string variable containing the URL query we want to parse
query = '?category=electronics&price_range=100-500'

# Use the built-in function "parse_qs" from the urllib.parse module to parse the query string
parsed = urllib.parse.parse_qs(query)

# Print the value of the "category" parameter from the parsed query string
# The [0] at the end is necessary because the parsed result is a list, and we want the first (and only) element
print(parsed['category'][0]) # Outputs "electronics"

That’s it! With just a few lines of code, we can easily extract the category parameter and its corresponding value from our query string. No more manually decoding those ***** strings like a caveman!
3.8 also introduces some new syntax for working with query parameters in Python. Instead of using `parse_qs()` to convert your query string into a dictionary-like object, you can now use f-strings (a.k.a., formatted strings) to extract specific values directly from the URL!
Here’s an example:

# Import the necessary modules
import urllib.parse # Import the urllib.parse module to work with URLs
from urllib.parse import urlparse # Import the urlparse function from the urllib.parse module

# Define the URL to be parsed
url = 'https://example.com/?category=electronics&price_range=100-500'

# Parse the URL using the urlparse function
parsed = urlparse(url)

# Get the query string from the parsed URL
query = parsed.query

# Create an empty dictionary to store the parameters and their values
params = {}

# Loop through each parameter/value pair in the query string
for param in query[1:].split('&'): # Remove the first character (the "?") and split by "&" to get each parameter/value pair
    key, value = param.split('=') # Split the parameter and value by "="
    params[key] = urllib.parse.unquote(value) # Decode any URL-encoded values using unquote() and store them in the dictionary

# Print the value of the "category" parameter
print(params['category']) # Outputs "electronics"

This may seem like a small improvement, but it can save you a lot of time and headaches when working with complex query strings! Plus, it’s just plain cool to see Python evolving and adding new features that make our lives easier. So go ahead, give it a try and let us know what you think in the comments below!
Later!

SICORPS