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?
gliemezis
  • 778
  • 2
  • 6
  • 18
  • 2
    They are both empty. b is just the notation for byte. – What Apr 27 '18 at 22:00
  • The issue is that `bool(str(b""))` is `True` in 3.6 but `False` in 2.7 – gliemezis Apr 27 '18 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. – Josh J Apr 27 '18 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. – progmatico Apr 27 '18 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. – kindall Apr 27 '18 at 22:29
  • Decode the bytestring, e.g. with `bool(b''.decode('utf-8'))` – L3viathan Apr 28 '18 at 20:51

0 Answers0