python: extended ASCII codes

前端 未结 4 912
太阳男子
太阳男子 2021-01-11 19:51

Hi I want to know how I can append and then print extended ASCII codes in python. I have the following.

code = chr(247)

li = []
li.append(code)
print li
         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-11 20:06

    You are doing nothing wrong.

    What you do is to add a string of length 1 to a list.

    This string contains a character outside the range of printable characters, and outside of ASCII (which is only 7 bit). That's why its representation looks like '\xf7'.

    If you print it, it will be transformed as good as the system can.

    In Python 2, the byte will be just printed. The resulting output may be the division symbol, or any other thing, according to what your system's encoding is.

    In Python 3, it is a unicode character and will be processed according to how stdout is set up. Normally, this indeed should be the division symbol.

    In a representation of a list, the __repr__() of the string is called, leading to what you see.

提交回复
热议问题