code: def encode(string, charset="utf-8", encoding=None, lang=""):
      """Encode string using the CTE encoding that produces the shorter result.
  
      Produces an RFC 2047/2243 encoded word of the form:
  
          =?charset*lang?cte?encoded_string?=
  
      where '*lang' is omitted unless the 'lang' parameter is given a value.
      Optional argument charset (defaults to utf-8) specifies the charset to use
      to encode the string to binary before CTE encoding it.  Optional argument
      'encoding' is the cte specifier for the encoding that should be used ('q'
      or 'b'); if it is None (the default) the encoding which produces the
      shortest encoded sequence is used, except that 'q' is preferred if it is up
      to five characters longer.  Optional argument 'lang' (default '') gives the
      RFC 2243 language string to specify in the encoded word.
  
      """
      if charset == "unknown-8bit":
          bstring = string.encode("ascii", "surrogateescape")
      else:
          bstring = string.encode(charset)
      if encoding is None:
          qlen = _cte_encode_length["q"](bstring)
          blen = _cte_encode_length["b"](bstring)
          # Bias toward q.  5 is arbitrary.
          encoding = "q" if qlen - blen < 5 else "b"
      encoded = _cte_encoders[encoding](bstring)
      if lang:
          lang = "*" + lang
      return "=?{}{}?{}?{}?=".format(charset, lang, encoding, encoded)
  
file_path: catboost/contrib/tools/python3/src/Lib/email/_encoded_words.py
imports: import re
  import base64
  import binascii
  import functools
  from string import ascii_letters, digits
  from email import errors
label_desc: Use image node_id 8 for calling a global function with example usage: encode(string, charset, encoding, lang) and returns: str