urlstd.parse.urlencode#

urlstd.parse.urlencode(query: Sequence[tuple[str, str]], encoding: str = 'utf-8') str#

An alternative to urllib.parse.urlencode().

Converts a sequence of tuples of name-value pairs into a percent-encoded ASCII text string in the form application/x-www-form-urlencoded.

Invalid surrogates will be replaced with U+FFFD. Also, if the encoding fails, it will be replaced with the appropriate XML character reference.

Parameters:
  • query – A sequence of tuples of name-value pairs to percent-encode.

  • encoding – The encoding to encode query.

Returns:

A string in the form application/x-www-form-urlencoded.

Examples

>>> urlencode([('a', 'a'), ('a', 'b'), ('a', 'c')])
'a=a&a=b&a=c'
>>> urlencode([('🌈', 'a')])
'%F0%9F%8C%88=a'
>>> urlencode([('🌈', 'a')], encoding="windows-1252")
'%26%23127752%3B=a'  # → '🌈=a'
>>> urlencode([('\ud83c\udf08', 'a')])
'%F0%9F%8C%88=a'
>>> urlencode([('\ud83c', 'a')])
'%EF%BF%BD=a'  # → '\ufffd=a'