Why does urllib.urlencode append "25" to the result?

1.5k Views Asked by At

The Python code is:

user = "aabc" 
password = "yyy12%"
data = urllib.urlencode({"loginname": user, "nloginpwd": password})
print data

The result is: loginname=aabc&nloginpwd=yyy12%25

Why was 25 added to the end of the string?

2

There are 2 best solutions below

0
Martijn Pieters On

The % character has special meaning in a URL; it is used to start an escape sequence. See the Percent-encoding article on Wikipedia. A literal % then has to be encoded too, and %25 is the encoded version. From the Wikipedia page:

Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI.

In other words, the %25 is an encoded % character.

0
vaultah On

From Wikipedia:

Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI.

Simple example:

>>> urllib.parse.quote('%')
'%25'