I use Python 3 and the requests module/library for querying a REST service.
It seems that requests by default uses urllib.parse.quote_plus()
for urlencoding, i.e. spaces are converted to +
.
However the REST service I query misinterpretes this as and
. So I need to encode spaces as %20
, i.e. use urllib.parse.quote()
instead.
Is there an easy way to do this with requests? I couldn't find any option in the documentation.
The reason is that a single quote or double quote itself is a special character we use in our Python program. In many cases, it has been seen that you want to print a string or you want to work with a string.
Actually, we can use triple quotes (i.e., triplet of single quotes or triplet double quotes) to represent the strings containing both single and double quotes to eliminate the need of escaping any. >>> print ('''She said, "Thank you!
If you want to print ‘WithQuotes’ in python, this can’t be done with only single (or double) quotes alone, it requires simultaneous use of both. # this code prints the output within quotes. The choice between both the types (single quotes and double quotes) depends on the programmer’s choice.
Put the escaping character before the single quote in the string. You can use the first solution like this: In Python, the backslash is an escaping character. So you can use it like the below: Now suppose you have to print the below line: She said, “You are looking nice”. And I smiled print ("She said, "You are looking nice". And I smiled")
It turns out you can!
from requests.utils import requote_uri
url = "https://www.somerandom.com/?name=Something Cool"
requote_uri(url)
'https://www.somerandom.com/?name=Something%20Cool'
documentation here The requote_uri
method is about halfway down the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With