Crafting Query Parameters in Python
Building URLs with query parameters is a fundamental skill in web development, essential for passing data to servers and interacting with APIs. This challenge asks you to create a Python function that dynamically constructs URLs with query parameters from a dictionary of key-value pairs. Mastering this skill is crucial for tasks like filtering search results, submitting form data, and interacting with various web services.
Problem Description
You are tasked with writing a Python function called create_query_string that takes a dictionary as input and returns a string representing the query string portion of a URL. The dictionary keys represent the parameter names, and the dictionary values represent the corresponding parameter values. The function should format the query string correctly, encoding special characters and ensuring proper separation between parameters.
What needs to be achieved:
- Convert a dictionary of key-value pairs into a URL query string.
- Encode special characters in both keys and values using URL encoding (e.g., spaces should be encoded as
%20). - Join the key-value pairs with an ampersand (&) to separate them.
- Prepend a question mark (?) to the beginning of the query string.
Key requirements:
- The function must handle empty dictionaries gracefully (returning "?").
- The function must handle keys and values of various data types (strings, numbers, booleans). Numbers and booleans should be converted to strings.
- The function must correctly encode spaces and other special characters.
Expected behavior:
The function should return a string that can be appended to a base URL to form a complete URL with query parameters.
Edge cases to consider:
- Empty dictionary input.
- Keys or values containing spaces or other special characters.
- Keys or values that are numbers or booleans.
- Multiple parameters with the same key (the last value should overwrite previous ones).
Examples
Example 1:
Input: {"name": "John Doe", "age": 30, "city": "New York"}
Output: "?name=John+Doe&age=30&city=New+York"
Explanation: The dictionary is converted to a query string with each key-value pair separated by '&'. Spaces in "John Doe" are encoded as '+'.
Example 2:
Input: {"search": "python programming", "page": 2}
Output: "?search=python+programming&page=2"
Explanation: The dictionary is converted to a query string. The space in "python programming" is encoded as '+'.
Example 3:
Input: {}
Output: "?"
Explanation: An empty dictionary results in a query string consisting only of the question mark.
Example 4:
Input: {"query": "hello world!", "version": 1.0}
Output: "?query=hello+world%21&version=1.0"
Explanation: The exclamation mark in "hello world!" is URL encoded as '%21'. The float 1.0 is converted to the string "1.0".
Constraints
- The input dictionary will only contain string keys and values of primitive data types (strings, numbers, booleans).
- The length of the query string will not exceed 2048 characters.
- The function must be efficient enough to handle dictionaries with up to 100 key-value pairs.
Notes
- You can use the
urllib.parse.quotefunction from the Python standard library to URL encode strings. This is the recommended approach for handling special characters. - Consider using a loop to iterate through the dictionary and build the query string incrementally.
- Remember to handle the case where the dictionary is empty.
- Pay close attention to the order of operations: URL encoding should be applied before joining the key-value pairs.
- The '+' character is used for space encoding in query strings, not '%20'.