Converting non English words to % separated string in Python
python, python-3.x, replace, string
Solution
Use `urllib.parse.quote`:
>>> import urllib.parse
>>> word = 'میباشد'
>>> urllib.parse.quote(word, encoding='utf-8')
'%D9%85%DB%8C%E2%80%8C%D8%A8%D8%A7%D8%B4%D8%AF'
You can omit `encoding='utf-8'` because `utf-8` encoding is used by default.
Problem
I have a Persian word like this: `word = میباشد`. If I run this: ``` word.encode(encoding='utf-8') ``` I see this in Python IDLE: ``` b'\xd9\x85\xdb\x8c\xe2\x80\x8c\xd8\xa8\xd8\xa7\xd8\xb4\xd8\xaf' ``` I want to convert the above line to a string that removes `b'` and replaces all `\x` with `%`. So I want to get this string: ``` %d9%85%db%8c%e2%80%8c%d8%a8%d8%a7%d8%b4%d8%af ``` What is the best way to do this in Python 3? Thanks for the help.