1. byes_to_str

def to_str(bytes_or_str):
  if isinstance(bytes_or_str, bytes):
    value = bytes_or_str.decode('utf-8')
  else:
    value = bytes_or_str 
  return value # Instance of str 
#
print(repr(to_str(b'foo'))) 
print(repr(to_str('bar')))

2. str_to_bytes

def to_bytes(bytes_or_str):
  if isinstance(bytes_or_str, str):
    value = bytes_or_str.encode('utf-8')
  else:
    value = bytes_or_str 
  return value # Instance of bytes 
#
print(repr(to_bytes(b'foo'))) 
print(repr(to_bytes('bar')))
Last modification:July 16th, 2021 at 05:44 pm