1

I am able to do the following to build a url:

base_url = 'http://google.com/'
qs = urllib.urlencode({'q': string})
url = base_url + '?' + qs

Is there a way to url-encode a string? For example, I would like to be able to do:

url = 'http://google.com/?q=' + urlencode('this is my search'))

1 Answer 1

4

Use urllib.quote or urllib.quote_plus, eg:

>>> urllib.quote('this is my string')
'this%20is%20my%20string'

>>> urllib.quote_plus('this is my string')
'this+is+my+string'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.