In Python3 this expression evaluates as False
:
b"" == ""
while in Python2 this comparison is True
:
u"" == ""
Checking for identity with is
obviously fails in both cases.
But why would they implement such a behaviour in Python3?
bytes consists of sequences of 8-bit unsigned values, while str consists of sequences of Unicode code points that represent textual characters from human languages.
In Python IDE, usually, the byte string will be automatically decoded using “ASCII” when printed out, so that's why the first result is human-readable (b'Hi'). More often, Byte string should be represented as hex code (b'\x48\x69'), which can be found in any “ASCII” table.
Convert bytes to a string Using decode() method. Using str() function. Using codecs. decode() method.
bytes() takes three optional parameters: source (Optional) - source to initialize the array of bytes. encoding (Optional) - if the source is a string, the encoding of the string. errors (Optional) - if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)
In python 3 string is Unicode . The type used to hold text is str
and the type used to hold data is bytes
.
the
str
andbytes
types cannot be mixed, you must always explicitly convert between them. Usestr.encode()
to go fromstr
tobytes
, andbytes.decode()
to go from bytes tostr
.
Therefore, if you do b"".decode() == ""
you'll get True
:
>>> b"".decode() == "" True
For more info read Text Vs. Data Instead Of Unicode Vs. 8-bi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With