Search code examples
pythonurlencode

Parsing url string from '+' to '%2B'


I have url address where its extension needs to be in ASCII/UTF-8

a='sAE3DSRAfv+HG='

i need to convert above as this:

a='sAE3DSRAfv%2BHG%3D'

I searched but not able to get it.


Solution

  • Please see built-in method urllib.parse.quote()

    A very important task for the URL is its safe transmission. Its meaning must not change after you created it till it is received by the intended receiver. To achieve that end URL encoding was incorporated. See RFC 2396

    URL might contain non-ascii characters like cafés, López etc. Or it might contain symbols which have different meaning when put in the context of a URL. For example, # which signifies a bookmark. To ensure safe transmitting of such characters HTTP standards maintain that you quote the url at the point of origin. And URL is always present in quoted format to anyone else.

    I have put sample usage below.

    >>> import urllib.parse
    >>> a='sAE3DSRAfv+HG='
    >>> urllib.parse.quote(a)
    'sAE3DSRAfv%2BHG%3D'
    >>>