1

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"?

2
  • This style of encoding is generally for URLs, in which case you'd also need to encode the space (Marta%20Ga%C5%82szewska). Python UTF-8 encoding would generally look like Marta Ga\xc5\x82szewska. Can you explain what you're using this style of percent encoding for, and what rule you need for your encoding? Commented Jul 24, 2019 at 13:50
  • 1
    Yes, I'm trying to convert the string and use it in an HTTP request (the resulting string has to be: Marta+Ga%C5%82szewska or Marta%20Ga%C5%82szewska). Encoding the string in "utf-8" gives me: "Marta Ga\xc5\x82szewska" but I can'f figure any way to get end convert the '\xc5' and '\x82' characters in '%c5' anf '%82'. Commented Jul 24, 2019 at 13:57

1 Answer 1

1

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' 
Sign up to request clarification or add additional context in comments.

1 Comment

This even does the utf-8 encoding, can't get much simpler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.