5

How can I encode a string so that I can pass the encoded string in url params?

0

3 Answers 3

8

use urllib.quote_plus function. Here is an example:

#!/usr/bin/python

print "Content-Type: text/html;charset=utf-8";
print

import urllib

str = "Hello World?"
str = urllib.quote_plus(str)

print str

Output: Hello+World%3F

Sign up to request clarification or add additional context in comments.

1 Comment

Python 3: str = urllib.parse.quote_plus(str)
0

The library too use is urllib, see http://docs.python.org/2/library/urllib.html

>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'

>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')
'string_of_characters_like_these%3A%24%23%40%3D%3F%25%5EQ%5E%24'

Comments

0

Use urllib.encode()

>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.