I think you are looking for urllib.pathname2url . For comparison:
Python 3, urllib.parse.quote :
>>> urllib.parse.quote('abc def/foo?bar=baz') 'abc%20def/foo%3Fbar%3Dbaz'
Python 2, urllib.pathname2url :
>>> urllib.pathname2url('abc def/foo?bar=baz') 'abc%20def/foo%3Fbar%3Dbaz'
The behavior is similar to me, but they may be a little different.
Edit:
After reading your comment on Algina's post, I think this is my preferred way of creating the URL:
>>> url = 'http://dev.echonest.com/api/v4/song/search' >>> params = {'api_key': 'xxxx', 'format': 'json', 'artist': 'Galaxie 500'} >>> "{}?{}".format(url, urllib.urlencode(params)) 'http://dev.echonest.com/api/v4/song/search?api_key=xxxx&artist=Galaxie+500&format=json'
source share