code: def encode_basestring(s, _PY3=PY3, _q=u('"')):
      
      if _PY3:
          if isinstance(s, binary_type):
              s = s.decode('utf-8')
      else:
          if isinstance(s, str) and HAS_UTF8.search(s) is not None:
              s = s.decode('utf-8')
      def replace(match):
          return ESCAPE_DCT[match.group(0)]
      return _q + ESCAPE.sub(replace, s) + _q
docs: Return a JSON representation of a Python string