Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does comparison of bytes with str fails in Python3

Tags:

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?

like image 366
steffen Avatar asked Jun 01 '15 18:06

steffen


People also ask

What is the difference between STR and bytes in Python?

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.

How are bytes represented in Python?

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.

Which method is used to convert raw byte data to a string in Python?

Convert bytes to a string Using decode() method. Using str() function. Using codecs. decode() method.

How do you initialize a bytes string in Python?

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)


1 Answers

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 and bytes types cannot be mixed, you must always explicitly convert between them. Use str.encode() to go from str to bytes, and bytes.decode() to go from bytes to str.

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

like image 190
Mazdak Avatar answered Sep 28 '22 05:09

Mazdak