+ 4

How do I get a character from its Unicode index number in Python?

In JavaScript, you probably know there is something called `String.fromCharCode();`. Is there a Python equivalent of that? (I need it to involve Unicode, not ASCII.) Thanks a lot!

2nd Apr 2018, 2:57 AM
Erik Johanson
Erik Johanson - avatar
3 Réponses
+ 3
ChaoticDawg: 'u' prefix for unicode string is only required for Python before version 3. Since Python3, strings are default unicode (and you need prefix litteral string with 'b' to define binary/ascii strings, wich are the default string type before version 3 ^^ Erik Johanson: The function you are looking for is unichr() if you use Python before version 3: https://docs.python.org/2/library/functions.html#unichr Else, for Python from version 3, you simply use the chr() function (as since Python3 it would work with unicode range values, what wasn't the case before)...
2nd Apr 2018, 10:56 AM
visph
visph - avatar
+ 3
Rafael Kohn: To optimize the probabilities to get an accurate answer, I would suggest you to create a new thread with similar well formed question but tagged with the targeted language you need, as this one is specified about Python ( personnaly I don't well know C# and so I cannot help you... ) ;)
8th Apr 2018, 2:48 PM
visph
visph - avatar
+ 2
If you just need to print it out all you need to do is have the unicode in the string like so: print(u'\u002B') # outputs + for multiples you just chain them together: print(u'\u002B\u003E') # outputs +> You can also store them in a variable and concatenate them to a string: a = u'\u002B\u003E' print("This string is concatenated to the unicode characters " + a) More info: https://docs.python.org/3/howto/unicode.html#the-unicode-type
2nd Apr 2018, 3:26 AM
ChaoticDawg
ChaoticDawg - avatar