DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Bytes in Python (2)

Buy Me a Coffee

*Memo:

A bytes can be used with len() to get the length as shown below:

v = b"Let's go!"

print(len(v))
# 9
Enter fullscreen mode Exit fullscreen mode

A non-empty bytes and empty bytes are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not keyword respectively.
# Non-empty bytes
print(bool(b'0'))
print(bool(b' '))
# True

# Empty bytes
print(bool(b''))
# False
Enter fullscreen mode Exit fullscreen mode
# Non-empty bytes
print(not b'0') # bytes
print(not b' ') # bytes
# False

# Empty bytes
print(not b'')
# True
Enter fullscreen mode Exit fullscreen mode

A bytes object can be checked if specific bytes are and aren't in the bytes object with in keyword and with not and in keyword respectively as shown below:

v = b'ABC'

print(b'A' in v)
print(b'BC' in v)
print(b'ABC' in v)
print(b'' in v)
# True

print(b'a' in v)
# False
Enter fullscreen mode Exit fullscreen mode
v = b'ABC'

print(b'A' not in v)
print(b'BC' not in v)
print(b'ABC' not in v)
print(b'' not in v)
# False

print(b'a' not in v)
# True
Enter fullscreen mode Exit fullscreen mode

A bytes object can be checked if the bytes object is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:

*Memo:

  • Be careful, bytes literals with is keyword and with is and not keyword get warnings so use == and != respectively.
v1 = b'ABC'
v2 = b'ABC'
v3 = v1

print(v1 is v2) # False
print(v1 is v3) # True

print(v1 is not v2) # True
print(v1 is not v3) # False
Enter fullscreen mode Exit fullscreen mode
print(b'ABC' is b'ABC') # True
print(b'ABC' is b'AB')  # False
# SyntaxWarning: "is" with 'bytes' literal. Did you mean "=="?

print(b'ABC' is not b'ABC') # False
print(b'ABC' is not b'AB')  # True
# SyntaxWarning: "is not" with 'bytes' literal. Did you mean "!="?
Enter fullscreen mode Exit fullscreen mode
print(b'ABC' == b'ABC') # True
print(b'ABC' == b'AB')  # False

print(b'ABC' != b'ABC') # False
print(b'ABC' != b'AB')  # True
Enter fullscreen mode Exit fullscreen mode

A bytes object and other bytes object can be checked if all the bytes in:

  • them are and aren't the same with == and != respectively.
  • the bytes object are in other bytes object with <=.
  • other bytes object are in the bytes object with >=.
  • the bytes object and other bytes are in other bytes object with <.
  • other bytes object and other bytes are in the bytes object with >.
v = b'ABC'

print(v == b'ABC')  # True
print(v == b'AB')   # False
print(v == b'AD')   # False
print(v == b'ABCD') # False
print(v == b'ABDE') # False
print(v == b'')     # False
Enter fullscreen mode Exit fullscreen mode
v = b'ABC'

print(v != b'ABC')  # False
print(v != b'AB')   # True
print(v != b'AD')   # True
print(v != b'ABCD') # True
print(v != b'ABDE') # True
print(v != b'')     # True
Enter fullscreen mode Exit fullscreen mode
v = b'ABC'

print(v <= b'ABC')  # True
print(v <= b'AB')   # False
print(v <= b'AD')   # True
print(v <= b'ABCD') # True
print(v <= b'ABDE') # True
print(v <= b'')     # False
Enter fullscreen mode Exit fullscreen mode
v = b'ABC'

print(v >= b'ABC')  # True
print(v >= b'AB')   # True
print(v >= b'AD')   # False
print(v >= b'ABCD') # False
print(v >= b'ABDE') # False
print(v >= b'')     # True
Enter fullscreen mode Exit fullscreen mode
v = b'ABC'

print(v < b'ABC')  # False
print(v < b'AB')   # False
print(v < b'AD')   # True
print(v < b'ABCD') # True
print(v < b'ABDE') # True
print(v < b'')     # False
Enter fullscreen mode Exit fullscreen mode
v = b'ABC'

print(v > b'ABC')  # False
print(v > b'AB')   # True
print(v > b'AD')   # False
print(v > b'ABCD') # False
print(v > b'ABDE') # False
print(v > b'')     # True
Enter fullscreen mode Exit fullscreen mode

A bytes object and other bytes object cannot be checked if they have and don't have their common bytes with bool() and & and with not keyword and & respectively as shown below:

v = b'ABC'

print(bool(v & b'BD'))
print(not (v & b'BD'))
# TypeError: unsupported operand type(s) for &: 'bytes' and 'bytes'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)