How to urlencode a querystring in Python?

Posted on

Solving problem is about exposing yourself to as many situations as possible like How to urlencode a querystring in Python? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How to urlencode a querystring in Python?, which can be followed any time. Take easy to follow this discuss.

How to urlencode a querystring in Python?

I am trying to urlencode this string before I submit.

queryString = 'eventName=' + evt.fields["eventName"] + '&' + 'eventDescription=' + evt.fields["eventDescription"];
Asked By: James


Answer #1:

You need to pass your parameters into urlencode() as either a mapping (dict), or a sequence of 2-tuples, like:

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

Python 3 or above


>>> urllib.parse.urlencode(f)

Note that this does not do url encoding in the commonly used sense (look at the output). For that use urllib.parse.quote_plus.

Answered By: bgporter

Answer #2:

Python 2

What you’re looking for is urllib.quote_plus:

>>> urllib.quote_plus('string_of_characters_like_these:$#@=?%^Q^$')

Python 3

In Python 3, the urllib package has been broken into smaller components. You’ll use urllib.parse.quote_plus (note the parse child module)

import urllib.parse
Answered By: Ricky

Answer #3:

Try requests instead of urllib and you don’t need to bother with urlencode!

import requests
requests.get('', params=evt.fields)


If you need ordered name-value pairs or multiple values for a name then set params like so:

params=[('name1','value11'), ('name1','value12'), ('name2','value21'), ...]

instead of using a dictionary.

Answered By: Barney Szabolcs

Answer #4:


  • Python (version 2.7.2 )


  • You want to generate a urlencoded query string.
  • You have a dictionary or object containing the name-value pairs.
  • You want to be able to control the output ordering of the name-value pairs.


  • urllib.urlencode
  • urllib.quote_plus



The following is a complete solution, including how to deal with some pitfalls.

### ********************
## init python (version 2.7.2 )
import urllib
### ********************
## first setup a dictionary of name-value pairs
dict_name_value_pairs = {
  "bravo"   : "True != False",
  "alpha"   : "",
  "charlie" : "hello world",
  "delta"   : "1234567 !@#$%^&*",
  "echo"    : "",
### ********************
## setup an exact ordering for the name-value pairs
ary_ordered_names = []
### ********************
## show the output results
if('NO we DO NOT care about the ordering of name-value pairs'):
  queryString  = urllib.urlencode(dict_name_value_pairs)
  print queryString
if('YES we DO care about the ordering of name-value pairs'):
  queryString  = "&".join( [ item+'='+urllib.quote_plus(dict_name_value_pairs[item]) for item in ary_ordered_names ] )
  print queryString
Answered By: dreftymac

Answer #6:

Try this:


urlencode won’t work because it only works on dictionaries. quote_plus didn’t produce the correct output.

Answered By: Charlie

Answer #7:

Note that the urllib.urlencode does not always do the trick. The problem is that some services care about the order of arguments, which gets lost when you create the dictionary. For such cases, urllib.quote_plus is better, as Ricky suggested.

Answered By: user411279

Answer #8:

In Python 3, this worked with me

import urllib
Answered By: Mazen Aly

Leave a Reply

Your email address will not be published.