4

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

rpdev123
  • 218
  • 2
  • 3
  • 7

3 Answers3

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