Search code examples
pythonwindowsunicode

How to print high numbered unicode characters in python


i am trying to write a python program that prints music notes (like 𝅗𝅥 -> u1d15e). However, i cant quite get it to work.

Here is what i get using the following code

note = '\U0001d15e'
bytes = note.encode('utf-8')
print(bytes)

>>> b'\xf0\x9d\x85\x9e'

If i try to print the string directly i get

note = '\U0001d15e'
# bytes = note.encode('utf-8')
print(note)

Traceback (most recent call last):
  File "c:\___\___\___\file.py", line 34, in <module>
    print(note)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001d15e' in position 0: character maps to <undefined>

I am not sure what the problem is. I know that similar questions have been asked before, however the proposed solution did not work for me. Thank you for your help in advance :)

i am using python 3.10.4


Solution

  • My system doesn't like that representation either, but can directly put it into a string and print() it

    To me, this is some artifact of your system being Windows and you need to set your console to use UTF-8 instead of cp1252

    Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)

    >>> "𝅗𝅥" == "\U0001d15e"
    True
    >>> note = "𝅗𝅥"
    >>> note.encode()
    b'\xf0\x9d\x85\x9e'
    >>> print(note)
    𝅗𝅥