code: def to_binary_string(obj, encoding=None):
      
      if PY2:
          # Python 2
          if encoding is None:
              return str(obj)
          else:
              return obj.encode(encoding)
      else:
          # Python 3
          return bytes(obj, 'utf-8' if encoding is None else encoding)
docs: Convert `obj` to binary string (bytes in Python 3, str in Python 2)