Reputation: 8859
I found the Python 3 documentation on chr
and ord
to be a little unclear as to how they relate to the two main textual data types: str
and bytes
. Or maybe I'm overthinking it.
Here is what I think probably happens, but can you let me know if I'm right?
ord()
takes as input a single-character str
and returns an int
. The input is a str
just like any other str
in Python 3. In particular, it is not bytes
encoded in some specific Unicode format like UTF-8, rather it represents Unicode Code Points in Python's internal str
format.chr()
takes as input an int
, and returns a single character str
. The returned str
is just like any other str
in Python, and similarly is not a specific encoding using bytes
.ord()
or chr()
deal with bytes
, nor do they deal with specific Unicode formats like UTF-8, they are only dealing with Python's internal str
representation which deals more abstractly with Unicode Code Points.Upvotes: 7
Views: 7052
Reputation: 18950
You are right.
ord()
and chr()
deal only with single-character strings.
Their documentation is quite clear about that:
>>> help(ord)
ord(c, /)
Return the Unicode code point for a one-character string.
>>> help(chr)
chr(i, /)
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
Use str.encode
/ bytes.decode
for conversion to/from bytes.
Upvotes: 0