Is there a way to urlencode/urldecode a string in Python? I want to emphasize that I want a single string, not a dict
. I know of the function that does that to a dict
. I could easily construct out of it a function that does that for a string, but I'd rather not have to construct my own function. Does Python have this functionality built-in?
(Answers for both Python 2.7 and 3.3 would be appreciated.)
URL Encoding (Percent Encoding) URL encoding converts characters into a format that can be transmitted over the Internet. URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.
A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.
URL Encoding ExampleThe ASCII value of space character in decimal is 32 , which when converted to hex comes out to be 20 . Now we just precede the hexadecimal representation with a percent sign ( % ), which gives us the URL encoded value - %20 .
parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.
See the urllib
module (docs here), in particular the urllib.quote
and urllib.unquote
functions.
urllib.unquote will do the trick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With