code: def detect_encoding(string):
      
      assert isinstance(string, bytes)
      try:
          detected = chardet.detect(string)
          if detected:
              return detected.get('encoding') or 'utf-8'
      except Exception as e:
          pass
      return 'utf-8'
docs: Tries to detect the encoding of the passed string.
  
      Defaults to UTF-8.