Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb4e941

Browse files
committed
Add a str class entry to the "Text Sequence Type" section (issue #16209).
This commit also moves the documentation for the str built-in function to the new class entry. Links to :class:`str` now go to the class entry with the string methods immediately afterwards.
1 parent 2160218 commit bb4e941

6 files changed

Lines changed: 78 additions & 56 deletions

File tree

Doc/c-api/object.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ Object Protocol
160160
a string similar to that returned by :c:func:`PyObject_Repr` in Python 2.
161161
Called by the :func:`ascii` built-in function.
162162
163+
.. index:: string; PyObject_Str (C function)
163164
164-
.. c:function:: PyObject* PyObject_Str(PyObject *o)
165165
166-
.. index:: builtin: str
166+
.. c:function:: PyObject* PyObject_Str(PyObject *o)
167167
168168
Compute a string representation of object *o*. Returns the string
169169
representation on success, *NULL* on failure. This is the equivalent of the

Doc/extending/newtypes.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,12 @@ done. This can be done using the :c:func:`PyErr_Fetch` and
982982
}
983983

984984

985-
Object Presentation
986-
-------------------
987-
988985
.. index::
986+
single: string; object representation
989987
builtin: repr
990-
builtin: str
988+
989+
Object Presentation
990+
-------------------
991991

992992
In Python, there are two ways to generate a textual representation of an object:
993993
the :func:`repr` function, and the :func:`str` function. (The :func:`print`

Doc/library/functions.rst

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ are always available. They are listed here in alphabetical order.
1414
:func:`all` :func:`dir` :func:`hex` :func:`next` :func:`slice`
1515
:func:`any` :func:`divmod` :func:`id` :func:`object` :func:`sorted`
1616
:func:`ascii` :func:`enumerate` :func:`input` :func:`oct` :func:`staticmethod`
17-
:func:`bin` :func:`eval` :func:`int` :func:`open` :func:`str`
17+
:func:`bin` :func:`eval` :func:`int` :func:`open` |func-str|_
1818
:func:`bool` :func:`exec` :func:`isinstance` :func:`ord` :func:`sum`
1919
:func:`bytearray` :func:`filter` :func:`issubclass` :func:`pow` :func:`super`
2020
:func:`bytes` :func:`float` :func:`iter` :func:`print` |func-tuple|_
@@ -34,6 +34,7 @@ are always available. They are listed here in alphabetical order.
3434
.. |func-memoryview| replace:: ``memoryview()``
3535
.. |func-set| replace:: ``set()``
3636
.. |func-list| replace:: ``list()``
37+
.. |func-str| replace:: ``str()``
3738
.. |func-tuple| replace:: ``tuple()``
3839
.. |func-range| replace:: ``range()``
3940

@@ -521,12 +522,12 @@ are always available. They are listed here in alphabetical order.
521522

522523
The float type is described in :ref:`typesnumeric`.
523524

524-
525-
.. function:: format(value[, format_spec])
526-
527525
.. index::
528-
pair: str; format
529526
single: __format__
527+
single: string; format() (built-in function)
528+
529+
530+
.. function:: format(value[, format_spec])
530531

531532
Convert a *value* to a "formatted" representation, as controlled by
532533
*format_spec*. The interpretation of *format_spec* will depend on the type
@@ -1238,44 +1239,12 @@ are always available. They are listed here in alphabetical order.
12381239
.. _func-str:
12391240
.. function:: str(object='')
12401241
str(object=b'', encoding='utf-8', errors='strict')
1242+
:noindex:
12411243

1242-
Return a :ref:`string <textseq>` version of *object*. If *object* is not
1243-
provided, returns the empty string. Otherwise, the behavior of ``str()``
1244-
depends on whether *encoding* or *errors* is given, as follows.
1245-
1246-
If neither *encoding* nor *errors* is given, ``str(object)`` returns
1247-
:meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely
1248-
printable string representation of *object*. For string objects, this is
1249-
the string itself. If *object* does not have a :meth:`~object.__str__`
1250-
method, then :func:`str` falls back to returning
1251-
:meth:`repr(object) <repr>`.
1244+
Return a :class:`str` version of *object*. See :func:`str` for details.
12521245

1253-
.. index::
1254-
single: buffer protocol; str() (built-in function)
1255-
single: bytes; str() (built-in function)
1256-
1257-
If at least one of *encoding* or *errors* is given, *object* should be a
1258-
:class:`bytes` or :class:`bytearray` object, or more generally any object
1259-
that supports the :ref:`buffer protocol <bufferobjects>`. In this case, if
1260-
*object* is a :class:`bytes` (or :class:`bytearray`) object, then
1261-
``str(bytes, encoding, errors)`` is equivalent to
1262-
:meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes
1263-
object underlying the buffer object is obtained before calling
1264-
:meth:`bytes.decode`. See :ref:`binaryseq` and
1265-
:ref:`bufferobjects` for information on buffer objects.
1266-
1267-
Passing a :class:`bytes` object to :func:`str` without the *encoding*
1268-
or *errors* arguments falls under the first case of returning the informal
1269-
string representation (see also the :option:`-b` command-line option to
1270-
Python). For example::
1271-
1272-
>>> str(b'Zoot!')
1273-
"b'Zoot!'"
1274-
1275-
``str`` is a built-in :term:`type`. For more information on the string
1276-
type and its methods, see the :ref:`textseq` and :ref:`string-methods`
1277-
sections. To output formatted strings, see the :ref:`string-formatting`
1278-
section. In addition, see the :ref:`stringservices` section.
1246+
``str`` is the built-in string :term:`class`. For general information
1247+
about strings, see :ref:`textseq`.
12791248

12801249

12811250
.. function:: sum(iterable[, start])

Doc/library/stdtypes.rst

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ range(2, 1, 3)`` or ``range(0, 3, 2) == range(0, 4, 2)``.)
13481348

13491349
.. index::
13501350
single: string; text sequence type
1351-
single: str() (built-in function); (see also string)
1351+
single: str (built-in class); (see also string)
13521352
object: string
13531353

13541354
.. _textseq:
@@ -1376,8 +1376,8 @@ See :ref:`strings` for more about the various forms of string literal,
13761376
including supported escape sequences, and the ``r`` ("raw") prefix that
13771377
disables most escape sequence processing.
13781378

1379-
Strings may also be created from other objects with the built-in
1380-
function :func:`str`.
1379+
Strings may also be created from other objects using the :class:`str`
1380+
constructor.
13811381

13821382
Since there is no separate "character" type, indexing a string produces
13831383
strings of length 1. That is, for a non-empty string *s*, ``s[0] == s[0:1]``.
@@ -1394,13 +1394,61 @@ multiple fragments.
13941394
once again permitted on string literals. It has no effect on the meaning
13951395
of string literals and cannot be combined with the ``r`` prefix.
13961396

1397+
1398+
.. index::
1399+
single: string; str (built-in class)
1400+
1401+
.. class:: str(object='')
1402+
str(object=b'', encoding='utf-8', errors='strict')
1403+
1404+
Return a :ref:`string <textseq>` version of *object*. If *object* is not
1405+
provided, returns the empty string. Otherwise, the behavior of ``str()``
1406+
depends on whether *encoding* or *errors* is given, as follows.
1407+
1408+
If neither *encoding* nor *errors* is given, ``str(object)`` returns
1409+
:meth:`object.__str__() <object.__str__>`, which is the "informal" or nicely
1410+
printable string representation of *object*. For string objects, this is
1411+
the string itself. If *object* does not have a :meth:`~object.__str__`
1412+
method, then :func:`str` falls back to returning
1413+
:meth:`repr(object) <repr>`.
1414+
1415+
.. index::
1416+
single: buffer protocol; str (built-in class)
1417+
single: bytes; str (built-in class)
1418+
1419+
If at least one of *encoding* or *errors* is given, *object* should be a
1420+
:class:`bytes` or :class:`bytearray` object, or more generally any object
1421+
that supports the :ref:`buffer protocol <bufferobjects>`. In this case, if
1422+
*object* is a :class:`bytes` (or :class:`bytearray`) object, then
1423+
``str(bytes, encoding, errors)`` is equivalent to
1424+
:meth:`bytes.decode(encoding, errors) <bytes.decode>`. Otherwise, the bytes
1425+
object underlying the buffer object is obtained before calling
1426+
:meth:`bytes.decode`. See :ref:`binaryseq` and
1427+
:ref:`bufferobjects` for information on buffer objects.
1428+
1429+
Passing a :class:`bytes` object to :func:`str` without the *encoding*
1430+
or *errors* arguments falls under the first case of returning the informal
1431+
string representation (see also the :option:`-b` command-line option to
1432+
Python). For example::
1433+
1434+
>>> str(b'Zoot!')
1435+
"b'Zoot!'"
1436+
1437+
For more information on the ``str`` class and its methods, see
1438+
:ref:`textseq` and the :ref:`string-methods` section below. To output
1439+
formatted strings, see the :ref:`string-formatting` section. In addition,
1440+
see the :ref:`stringservices` section.
1441+
1442+
1443+
.. index::
1444+
pair: string; methods
1445+
13971446
.. _string-methods:
13981447

13991448
String Methods
14001449
--------------
14011450

14021451
.. index::
1403-
pair: string; methods
14041452
module: re
14051453

14061454
Strings implement all of the :ref:`common <typesseq-common>` sequence

Doc/reference/datamodel.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,13 @@ Sequences
274274

275275
The following types are immutable sequences:
276276

277+
.. index::
278+
single: string; immutable sequences
279+
277280
Strings
278281
.. index::
279282
builtin: chr
280283
builtin: ord
281-
builtin: str
282284
single: character
283285
single: integer
284286
single: Unicode
@@ -1188,14 +1190,14 @@ Basic customization
11881190
Called by :func:`bytes` to compute a byte-string representation of an
11891191
object. This should return a ``bytes`` object.
11901192

1191-
1192-
.. method:: object.__format__(self, format_spec)
1193-
11941193
.. index::
1194+
single: string; __format__() (object method)
11951195
pair: string; conversion
1196-
builtin: str
11971196
builtin: print
11981197

1198+
1199+
.. method:: object.__format__(self, format_spec)
1200+
11991201
Called by the :func:`format` built-in function (and by extension, the
12001202
:meth:`str.format` method of class :class:`str`) to produce a "formatted"
12011203
string representation of an object. The ``format_spec`` argument is

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ Tools/Demos
292292
Documentation
293293
-------------
294294

295+
- Issue #16209: Move the documentation for the str built-in function to a new
296+
str class entry in the "Text Sequence Type" section.
297+
295298
- Issue #13538: Improve str() and object.__str__() documentation.
296299

297300
- Issue #16489: Make it clearer that importlib.find_loader() requires any and

0 commit comments

Comments
 (0)