Search code examples
python-3.xencodingutf-8decodingunicode-string

How to convert a string with unicode characters in a string with utf-8 hex characters?


I'm trying (without success) to convert the following string (it has the ł ={LATIN SMALL LETTER L WITH STROKE} character encodede in unicode):

Marta Ga\u0142szewska

in the following utf-8 hex form:

Marta Ga%C5%82uszewska

How I can achieve that conversion using Python and store the result in a variable like variable = "Marta Ga%C5%82uszewska"?


Solution

  • For URL-encoding, you want urllib.parse.quote:

    import urllib.parse
    s = "Marta Ga\u0142szewska"
    q = urllib.parse.quote(s)
    
    => 'Marta%20Ga%C5%82szewska'
    

    If you prefer + to %20, you can use quote_plus:

    q = urllib.parse.quote_plus(s)
    
    => 'Marta+Ga%C5%82szewska'