2

Python 2.7.13

>>> str(b'')
""
>>> bool(str(b''))
False

Python 3.6.0

>>> str(b'')
"b''"
>>> bool(str(b''))
True

My question is two-fold:

  • Why does this happen?
  • What is the best way to get an empty string from an empty byte string in Python 3.6.0?
6
  • 2
    They are both empty. b is just the notation for byte. Commented Apr 27, 2018 at 22:00
  • The issue is that bool(str(b"")) is True in 3.6 but False in 2.7 Commented Apr 27, 2018 at 22:06
  • Something is wrong here with your example. In the 3.6 version your string contains the string b single quote single quote. Commented Apr 27, 2018 at 22:09
  • Nothng is wrong with the example. Because the string contains that non empty value, bool() returns True. Difference is not in bool but in Python2 vs Python3 differences relating binary strings treatment. Commented Apr 27, 2018 at 22:21
  • Take off the bool() and see what str(b"") is in the two versions. Hint: it's three characters long in Python 3. Commented Apr 27, 2018 at 22:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.