Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL-encoding and -decoding a string in Python

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.)

like image 756
Ram Rachum Avatar asked Jan 10 '14 10:01

Ram Rachum


People also ask

What is URL encoded string?

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.

What is a %20 in a URL?

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.

What is URL encoding give example?

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 .

What does Urllib parse URL encode do?

parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.


2 Answers

See the urllib module (docs here), in particular the urllib.quote and urllib.unquote functions.

like image 51
Keeler Avatar answered Oct 22 '22 23:10

Keeler


urllib.unquote will do the trick

like image 36
Raul Andres Avatar answered Oct 22 '22 23:10

Raul Andres