Name
urlencode
Synopsis
urlencode(query,doseq=False)
Returns a string with the URL-encoded form of
query. query
can be either a sequence of
(
name,
value
) pairs, or a
mapping, in which case the resulting string encodes the
mapping’s
(
key,
value
) pairs. For
example:
urllib.urlencode([('ans',42),('key','val')])
# 'ans=42&key=val'
urllib.urlencode({'ans':42, 'key':'val'})
# 'key=val&ans=42'Remember that the order of items in a dictionary is not defined: if
you need the URL-encoded form to have the key/value pairs in a
specific order, use a sequence as the
query argument, as in the first call in
this example.
When doseq is true, any
value in query
that is a sequence is encoded as separate parameters, one per item in
value. For example:
u.urlencode([('K',('x','y','z'))],1)
# 'K=x&K=y&K=z'
u.urlencode([('K',('x','y','z'))],0)
# 'K=%28%27x%27%2C+%27y%27%2C+%27z%27%29'When doseq is false (the default), each
value is encoded as the quote_plus of its string
form given by built-in str, whether the value is a
sequence or not.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access