'Why the readchar() return a byte, when it have to be a string
I have read the documentation about readchar, and there it says that the readchar method have to return a string, but when I run it returns bytes. I would like to understand why this happen, because I am new in the programming
a) This is the code I use to prove, and here say the data returned is a byte
x = readchar.readchar()
print(a)
print(type(x))
b) This is the output in my consola
b'a'
<class 'bytes'>
c) This is the readchar documentation https://pypi.org/project/readchar/ or https://github.com/magmax/python-readchar
d) This is what says in the doc about it
readchar() Reads the next char from stdin, returning it as a string with length 1.
PS. I made the same prove for readkey(), and this return a string according to the doc
Solution 1:[1]
It returns it, as a "byte chain" (which is the bytes of the UTF-8 chain, not displayed as characters, but as a "byte chain").
Solution 2:[2]
Read up on Python types: bytes, bytearray
A Python string is a sequence (list) of characters. If we stick to ASCII each character can be represented by an 8 bit number.
But we don't stick to ASCII. Unicode gives us öäü€ß¹ etc
A Python string containing Unicode characters can need more than 1 byte per character.
Raw devices like files, serial ports etc return a bytes object describing all the Unicode characters as bytes.
Now a Unicode byte buffer can be converted to a string by:
mystring = mybytes.decode('utf-8')
which can raise an Exception if the it's not a valid utf-8 encoding.
Why all this complication?
Because files and devices are inherently byte devices, and strings which seem simple to humans are complex beasts for the Python engine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Desktop Firework |
| Solution 2 | Francis Cagney |
