How can I encode a string so that I can pass the encoded string in url params?
Asked
Active
Viewed 1.0k times
3 Answers
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

Mosiur
- 1,342
- 13
- 16
-
6Python 3: `str = urllib.parse.quote_plus(str)` – runcoderun Feb 07 '19 at 22:04
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'

alvas
- 115,346
- 109
- 446
- 738
0
Use urllib.encode()
>>> import urllib
>>> f = { 'eventName' : 'myEvent', 'eventDescription' : "cool event"}
>>> urllib.urlencode(f)
'eventName=myEvent&eventDescription=cool+event'

Danstahr
- 4,190
- 22
- 38