@@ -534,7 +534,7 @@ are always available. They are listed here in alphabetical order.
534534 is used by most built-in types: :ref: `formatspec `.
535535
536536 The default *format_spec * is an empty string which usually gives the same
537- effect as calling `` str(value) ` `.
537+ effect as calling :func: ` str(value) <str> `.
538538
539539 A call to ``format(value, format_spec) `` is translated to
540540 ``type(value).__format__(format_spec) `` which bypasses the instance
@@ -1249,37 +1249,50 @@ are always available. They are listed here in alphabetical order.
12491249 For more information on static methods, consult the documentation on the
12501250 standard type hierarchy in :ref: `types `.
12511251
1252+ .. index ::
1253+ single: string; str() (built-in function)
1254+
12521255
12531256.. _func-str :
12541257.. function :: str(object='')
1255- str(object[, encoding[, errors]])
1256-
1257- Return a :ref: `string <textseq >` version of an object, using one of the
1258- following modes:
1259-
1260- If *encoding * and/or *errors * are given, :func: `str ` will decode the
1261- *object * which can either be a byte string or a character buffer using
1262- the codec for *encoding *. The *encoding * parameter is a string giving
1263- the name of an encoding; if the encoding is not known, :exc: `LookupError `
1264- is raised. Error handling is done according to *errors *; this specifies the
1265- treatment of characters which are invalid in the input encoding. If
1266- *errors * is ``'strict' `` (the default), a :exc: `ValueError ` is raised on
1267- errors, while a value of ``'ignore' `` causes errors to be silently ignored,
1268- and a value of ``'replace' `` causes the official Unicode replacement character,
1269- U+FFFD, to be used to replace input characters which cannot be decoded.
1270- See also the :mod: `codecs ` module.
1271-
1272- When only *object * is given, this returns its nicely printable representation.
1273- For strings, this is the string itself. The difference with ``repr(object) ``
1274- is that ``str(object) `` does not always attempt to return a string that is
1275- acceptable to :func: `eval `; its goal is to return a printable string.
1276- With no arguments, this returns the empty string.
1277-
1278- Objects can specify what ``str(object) `` returns by defining a :meth: `__str__ `
1279- special method.
1280-
1281- For more information on strings and string methods, see the :ref: `textseq `
1282- section. To output formatted strings, see the :ref: `string-formatting `
1258+ str(object=b'', encoding='utf-8', errors='strict')
1259+
1260+ Return a :ref: `string <textseq >` version of *object *. If *object * is not
1261+ provided, returns the empty string. Otherwise, the behavior of ``str() ``
1262+ depends on whether *encoding * or *errors * is given, as follows.
1263+
1264+ If neither *encoding * nor *errors * is given, ``str(object) `` returns
1265+ :meth: `object.__str__() <object.__str__> `, which is the "informal" or nicely
1266+ printable string representation of *object *. For string objects, this is
1267+ the string itself. If *object * does not have a :meth: `~object.__str__ `
1268+ method, then :func: `str ` falls back to returning
1269+ :meth: `repr(object) <repr> `.
1270+
1271+ .. index ::
1272+ single: buffer protocol; str() (built-in function)
1273+ single: bytes; str() (built-in function)
1274+
1275+ If at least one of *encoding * or *errors * is given, *object * should be a
1276+ :class: `bytes ` or :class: `bytearray ` object, or more generally any object
1277+ that supports the :ref: `buffer protocol <bufferobjects >`. In this case, if
1278+ *object * is a :class: `bytes ` (or :class: `bytearray `) object, then
1279+ ``str(bytes, encoding, errors) `` is equivalent to
1280+ :meth: `bytes.decode(encoding, errors) <bytes.decode> `. Otherwise, the bytes
1281+ object underlying the buffer object is obtained before calling
1282+ :meth: `bytes.decode `. See :ref: `binaryseq ` and
1283+ :ref: `bufferobjects ` for information on buffer objects.
1284+
1285+ Passing a :class: `bytes ` object to :func: `str ` without the *encoding *
1286+ or *errors * arguments falls under the first case of returning the informal
1287+ string representation (see also the :option: `-b ` command-line option to
1288+ Python). For example::
1289+
1290+ >>> str(b'Zoot!')
1291+ "b'Zoot!'"
1292+
1293+ ``str `` is a built-in :term: `type `. For more information on the string
1294+ type and its methods, see the :ref: `textseq ` and :ref: `string-methods `
1295+ sections. To output formatted strings, see the :ref: `string-formatting `
12831296 section. In addition, see the :ref: `stringservices ` section.
12841297
12851298
0 commit comments