Search code examples
pythonurlencode

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


Solution

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