Alexandra Bradan
Alexandra Bradan

Reputation: 185

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

Upvotes: 1

Views: 273

Answers (1)

Rob Napier
Rob Napier

Reputation: 299663

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'

Upvotes: 1

Related Questions