Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Python urlencode: Use doseq and quote_via for Query Strings

Use Python’s urllib.parse.urlencode to build query strings, expand sequence values with doseq=True, and choose whether spaces become + or %20.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use urllib.parse.urlencode to turn query data into an ampersand-separated, percent-encoded string. Set doseq=True when a value is a sequence that should produce repeated keys, and choose quote_via to control how spaces and slashes are encoded.

Build a query string with urlencode

urlencode accepts either a mapping or a sequence of two-item tuples, where each tuple contains a key and its value. It returns percent-encoded ASCII text with pairs separated by &. For example:

from urllib.parse import urlencode

params = {"q": "python urls", "page": 2}
print(urlencode(params))
# q=python+urls&page=2

The default encoding uses form-style quoting: spaces become +, and a slash in a key or value is encoded as %2F. Both keys and values are passed through the selected quoting function.

Use doseq=True to repeat a key for sequence values

With the default doseq=False, a sequence supplied as a value is treated as one value rather than expanded into multiple query pairs. Set doseq=True when you want a separate pair for each element:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from urllib.parse import urlencode

params = [("tag", ["python", "urls"]), ("page", 2)]

print(urlencode(params, doseq=True))
# tag=python&tag=urls&page=2

When the input is a sequence of tuples, the resulting pairs follow the tuple order. This makes tuple input useful when repeated keys or a specific parameter order matter.

Choose between quote_plus and quote

The quote_via argument selects the function that quotes keys and values. Its default is quote_plus. Pass urllib.parse.quote if you want spaces represented as %20 and slashes left unescaped by default:

from urllib.parse import quote, urlencode

params = {"path": "docs/python urls"}

print(urlencode(params))
# path=docs%2Fpython+urls

print(urlencode(params, quote_via=quote))
# path=docs/python%20urls
Option Space Slash (/) Typical choice
Default quote_plus + %2F Use when form-style plus-for-space encoding is wanted.
quote %20 Unescaped by default Use when percent-encoded spaces are wanted and slashes should remain unescaped.

The safe argument can further specify characters to leave unquoted. The safe, encoding, and errors arguments are passed to the quoting function; encoding and errors apply only to string query elements.

Parse the result back into Python data

Use urllib.parse.parse_qs or urllib.parse.parse_qsl to parse a query string. They are the documented counterparts for turning encoded query data back into Python structures. Choose the parser that fits the shape your code needs.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Python version notes

According to the Python library reference, urlencode has accepted bytes and string query values since Python 3.2, and quote_via was added in Python 3.5. The current documentation marks acceptance of certain false-valued objects as deprecated in Python 3.14, with exceptions including empty strings, byte-like objects, and None. Check the documentation for the specific Python release you support when relying on compatibility details.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.