6

I tried this: but it doesn't work.

print urllib.urlencode("http://"+SITE_DOMAIN+"/go/")

I want to turn it into a string with url encodings

1
  • urlencode() is for converting a sequence (eq tuple) or mapping (eq dict) object to a querystring. For future reference, the Python docs are a really good resource to use: docs.python.org/library/index.html Commented Mar 23, 2011 at 2:05

2 Answers 2

13

Were you looking for the quote() or quote_plus() function instead?

>>> urllib.quote("http://spam.com/go/")
'http%3A%2F%2Fspam.com%2Fgo%2F'
Sign up to request clarification or add additional context in comments.

3 Comments

+1 because quote_plus() is probably the one you'll really be using the most.
For python 3, these functions are in the urllib.parse package (instead of urllib).
The example is not correct for Python2. By default quote in Python2 treats "/" as safe and doesn't encode them.
1

You are looking for urllib.quote().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.