Search code examples
pythonstringpython-2.7string-interpolation

String Interpolation in Python


I am trying to interpolate this string correctly:

/test/test?Monetization%20Source=%d&Channel%20Id=%d' % (mid, cid)

I want the the %20 to rendered as is and the %d to serve as place-holderes for the variables mid and cid. How do I do this?


Solution

  • In general, you want urllib.urlencode:

    import urllib
    url = '/test/test?' + urllib.urlencode({
      'Monetization Source': mid,
      'Channel Id': cid,
    })