Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106318: Add string method examples #119445

Draft
wants to merge 67 commits into
base: main
Choose a base branch
from
Draft
Changes from 3 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
5508700
[Doc] Added snippet code to str.join method
adorilson May 11, 2021
e388d45
[Doc] Added snippet code to str.ljust method
adorilson May 12, 2021
d8407ba
[Doc] Added snippet code to str.rjust method
adorilson May 12, 2021
08b88ef
[Doc] Added 'See also' in str.lstrip and str.rstrip methods from one …
adorilson May 12, 2021
52e3a6b
[Doc] Added snippet code to str.maketrans method
adorilson May 12, 2021
dee059c
[Doc] Added snippet code to str.translate method
adorilson May 12, 2021
1671497
[Doc] Added snippet code to str.partition method
adorilson May 12, 2021
c832623
[Doc] Added snippet code to str.replace method
adorilson May 12, 2021
0be8170
[Doc] Added snippet code to str.rfind method
adorilson May 12, 2021
ff65bd2
[Doc] Added snippet code to str.rindex method
adorilson May 12, 2021
322c88c
[Doc] Added snippet code to str.rpartition method
adorilson May 12, 2021
3a473df
[DOC] minor change
adorilson May 12, 2021
b653693
[Doc] Added snippet code to str.rsplit method
adorilson May 12, 2021
1b412cb
[Doc] Added snippet code to str.startswith method
adorilson May 12, 2021
fb56441
[Doc] Added snippet code to str.swapcase method
adorilson May 12, 2021
04a5212
[Doc] Added snippet code to str.upper method
adorilson May 13, 2021
8771775
[DOC] Added snipped code in str.capitalize
adorilson Feb 27, 2021
f2e224c
[DOC] Add snippet code in str.casefold and str.lower methods
adorilson Feb 27, 2021
1d39f92
[DOC] Added snippet code in str.center method
adorilson Feb 27, 2021
b3a70b6
[DOC] Added snippet code in str.count method
adorilson Feb 27, 2021
37477f8
[DOC] fix typo
adorilson Feb 28, 2021
f9a7293
[DOC] Added snippet code in str.encode method
adorilson Mar 13, 2021
2e54cb0
[DOC] Added snippet code to str.endswith method
adorilson Mar 13, 2021
96d9bf6
[DOC] Added snippet code with \n to str.expandtabs method
adorilson Mar 13, 2021
d1dc7e0
[DOC] Added snippet code to str.index method
adorilson Mar 14, 2021
8661d8e
[DOC] Added snippet code to str.isalnum method
adorilson Mar 14, 2021
9e1f289
[DOC] Added snippet code in str.isalpha method
adorilson Mar 15, 2021
9e361d8
[DOC] Added snippet code in str.isascii method
adorilson Mar 15, 2021
09a463a
[DOC] Added snippet code to str.isdecimal method
adorilson Mar 15, 2021
641de5c
[Doc] Added snippet code to str.isdigit method
adorilson Mar 22, 2021
eae1564
[Doc] Added snippet code to str.islower method
adorilson Mar 22, 2021
58331b8
[Doc] Added snippet code to str.isnumeric method
adorilson Mar 22, 2021
d7ce39c
[Doc] Added snippet code to str.isprintable method
adorilson Mar 22, 2021
b86cefd
[Doc] Added snippet code to str.isspace method
adorilson Mar 22, 2021
3df630c
[Doc] Adde snippet code to str.istitle method
adorilson Mar 22, 2021
ae42fea
Minor changes (formatting and typo)
adorilson Jun 11, 2023
4676daa
[Doc] Making lint happy
adorilson Jun 11, 2023
7642f31
Merge branch 'main' into str_methods_2
adorilson Jul 3, 2023
de11c93
Update Doc/library/stdtypes.rst
adorilson Jul 3, 2023
1695b93
[DOC] Added some 'see also' to str methods
adorilson Jul 3, 2023
ad6b56b
[DOC] Moved 'For example' to the end of the previous paragraph
adorilson Jul 3, 2023
e4e1405
[DOC] Putting a space after # in comments on str methods
adorilson Jul 3, 2023
2dfa3f2
[DOC] Fixed inconsistent literal block quoting
adorilson Jul 3, 2023
27cc327
fix other comments
hauntsaninja Jul 3, 2023
13c01d7
Merge branch 'main' into str_methods_2
adorilson Sep 17, 2023
03bef0a
prepare for FF
blaisep May 22, 2024
9c8955e
update the worflows files
blaisep May 22, 2024
e44d39c
prepare for FF
blaisep May 22, 2024
925c0be
Use the examples created by @adorilson
blaisep May 22, 2024
679eac8
Correct typos in rST formatting
blaisep May 23, 2024
098d9f3
Merge pull request #1 from blaisep/pr-105670
blaisep May 24, 2024
7e7787e
Merge pull request #2 from blaisep/main
blaisep May 24, 2024
031bf40
Apply suggestions from code review
blaisep May 26, 2024
08fcf55
Update stdtypes.rst with edits to content
blaisep May 27, 2024
1c55459
Update unicode.rst
blaisep May 27, 2024
30398db
Update Doc/library/stdtypes.rst
blaisep May 28, 2024
f76afc9
Remove unrelated, conflicting files.
blaisep May 28, 2024
a0d7846
Merge remote-tracking branch 'upstream/main' into pr-105670
blaisep May 28, 2024
c82e92c
Fix trailing space and location of example.
blaisep May 28, 2024
4d70dcf
Adjust the placement of examples more meticulously.
blaisep May 28, 2024
48bed4c
Update Doc/library/stdtypes.rst
blaisep Jun 5, 2024
dce8f2b
Update Doc/library/stdtypes.rst
blaisep Jun 5, 2024
24c1f6a
Update Doc/library/stdtypes.rst
blaisep Jun 5, 2024
fb2fe01
WIP update some examples
blaisep Jun 10, 2024
0beabee
Merge branch 'pr-105670' of github.com:blaisep/cpython into pr-105670
blaisep Jun 10, 2024
02b2cd8
WIP Edit and change some examples
blaisep Jun 11, 2024
0dd8138
Extend the example for str.format_map
blaisep Jul 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 70 additions & 62 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1626,8 +1626,8 @@

>>> 'PYTHON IS AMAZING'.capitalize()
'Python is amazing'
>>> 'Njemačka Starts With a non-English Digraph'.capitalize()
'Njemačka starts with a non-English digraph'
>>> 'åhléns starts with a non-english digraph'.capitalize()
'Åhléns starts with a non-english digraph'

See also :meth:`title`.

Expand Down Expand Up @@ -1676,26 +1676,46 @@
.. method:: str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring *sub* in the
range [*start*, *end*]. Optional arguments *start* and *end* are
interpreted as in slice notation.

If *sub* is empty, returns the number of empty strings between characters
which is the length of the string plus one. These are examples of each case::
(optional) range [*start*, *end*]::

>>> 'spam, spam, spam'.count('spam')
>>> 'spam, spam, spam'.count('spam')
3

>>> 'spam, spam, spam'.count('spam', 5)
2

The arguments *start* and *end* are optional and interpreted as in :func:`slice` notation::



>>> 'spam, spam, spam'[5:10].count('spam')
1

Or, perhaps more readable alternative::

>>> 'spam, spam, spam'.count('spam', 5, 10)
1


>>> 'spam, spam, spam'.count('eggs')
0

If *sub* is empty, returns the number of empty strings between characters
which is the length of the string plus one. These are examples of each case::

>>> 'spam, spam, spam'.count('')
17

.. method:: str.encode(encoding="utf-8", errors="strict")

Return the string encoded to :class:`bytes`.
Return the string encoded to :class:`bytes` ::

>>> encoded_str_to_byte = 'Python'.encode()
>>> type(encoded_str_to_byte)
<class 'bytes'>
>>> encoded_str_to_byte
b'¿como estás?'
blaisep marked this conversation as resolved.
Show resolved Hide resolved


*encoding* defaults to ``'utf-8'``;
see :ref:`standard-encodings` for possible values.
Expand All @@ -1712,11 +1732,6 @@
:ref:`devmode` is enabled
or a :ref:`debug build <debug-build>` is used::

>>> encoded_str_to_byte = 'Python'.encode()
>>> type(encoded_str_to_byte)
<class 'bytes'>
>>> encoded_str_to_byte
b'Python'

.. versionchanged:: 3.1
Added support for keyword arguments.
Expand Down Expand Up @@ -1756,17 +1771,19 @@
Return a copy of the string where all tab characters are replaced by one or
more spaces, depending on the current column and the given tab size. Tab
positions occur every *tabsize* characters (default is 8, giving tab
positions at columns 0, 8, 16 and so on). To expand the string, the current
column is set to zero and the string is examined character by character. If
the character is a tab (``\t``), one or more space characters are inserted
in the result until the current column is equal to the next tab position.
(The tab character itself is not copied.)::
positions at columns 0, 8, 16 and so on).

>>> '01\t012\t0123\t01234'.expandtabs()
'01 012 0123 01234'
>>> '01\t012\t0123\t01234'.expandtabs(4)
'01 012 0123 01234'

To expand the string, the current
column is set to zero and the string is examined character by character. If
the character is a tab (``\t``), one or more space characters are inserted
in the result until the current column is equal to the next tab position.
(The tab character itself is not copied.)

If the character is a newline (``\n``) or return (``\r``), it is copied and the current column is reset to
zero::

Expand All @@ -1784,7 +1801,7 @@
Return the lowest index in the string where substring *sub* is found within
the slice ``s[start:end]``::

>>> 'spam, spam, spam'.find('sp')
>>> 'spam, spam, spam'.find(',')
0
blaisep marked this conversation as resolved.
Show resolved Hide resolved

Optional arguments *start* and *end* are interpreted as in slice notation::
Expand Down Expand Up @@ -1883,9 +1900,9 @@
in the Unicode character database as "Letter", i.e., those with general category
property being one of "Lm", "Lt", "Lu", "Ll", or "Lo"::

>>> 'a commom word'.isalpha()
>>> 'a common word'.isalpha()
False
>>> 'acommomword'.isalpha()
>>> 'acommonword'.isalpha()
True
>>> 'µ'.isalpha()
True
Expand All @@ -1910,11 +1927,6 @@
>>> 'acommomword'.isascii()
True
>>> 'µ'.isascii()
False
>>> 'æ'.isascii()
False
>>> 'Ŧ'.isascii()
False

ASCII characters have code points in the range U+0000-U+007F.

Expand All @@ -1934,8 +1946,8 @@

>>> '٠١٢٣٤٥٦٧٨٩'.isdecimal() # ARABIC-INDIC DIGIT ZERO to NINE
True
>>> '²'.isdecimal(), '²'.isdigit()
(False, True)
>>> '²'.isdecimal()
True
blaisep marked this conversation as resolved.
Show resolved Hide resolved

Formally a decimal character is a character in the Unicode General Category "Nd".
See also :meth:`isdigit`. Decimal numbers is a digit numbers subset.
Expand All @@ -1950,8 +1962,8 @@
True
>>> '٠١٢٣٤٥٦٧٨٩'.isdigit() # ARABIC-INDIC DIGIT ZERO to NINE
True
>>> '²'.isdigit(), '²'.isdecimal()
(True, False)
>>> '²'.isdigit()
True

This covers digits which cannot be used to form numbers in base 10,
like the Kharosthi numbers. Formally, a digit is a character that has the
Expand All @@ -1965,10 +1977,7 @@
definition, section :ref:`identifiers`.

:func:`keyword.iskeyword` can be used to test whether string ``s`` is a reserved
identifier, such as :keyword:`def` and :keyword:`class`.

Example:
::
identifier, such as :keyword:`def` and :keyword:`class`::

>>> from keyword import iskeyword

Expand Down Expand Up @@ -2006,7 +2015,6 @@
>>> '0123456789'.isnumeric()
True


Numeric characters include digit characters, and all characters
that have the Unicode numeric value property, e.g. U+2155,
VULGAR FRACTION ONE FIFTH::
Expand Down Expand Up @@ -2034,14 +2042,13 @@

>>> ''.isprintable()
True
>>> ' '.isprintable()
True

Nonprintable characters are those characters defined
Non-printable characters are those characters defined
in the Unicode character database as "Other" or "Separator", excepting the
ASCII space (0x20) which is considered printable::


>>> ' '.isprintable()
True
>>> '\t\n'.isprintable() # TAB and BREAK LINE
False
>>> '\u3000'.isprintable() # IDEOGRAPHIC SPACE
Expand All @@ -2058,7 +2065,7 @@
Return ``True`` if there are only whitespace characters in the string and there is
at least one character, ``False`` otherwise::

>>> ''.isspace()
>>> 'banana'.isspace()
False
>>> ' '.isspace()
True
Expand All @@ -2067,6 +2074,7 @@
>>> '\u3000'.isspace() # IDEOGRAPHIC SPACE
True


See also :meth:`isprintable`.

A character is *whitespace* if in the Unicode character database
Expand All @@ -2081,11 +2089,9 @@
character, for example uppercase characters may only follow uncased characters
and lowercase characters only cased ones. Return ``False`` otherwise::

>>> 'Spam, Spam, Spam'.istitle()
>>> 'Don Quixote'.istitle()
True
>>> 'spam, spam, spam'.istitle()
False
>>> 'SPAM, SPAM, SPAM'.istitle()
>>> 'Don quixote'.istitle()
False

See also :meth:`title`.
Expand Down Expand Up @@ -2114,10 +2120,10 @@

Return a string which is the concatenation of the strings in *iterable*.
A :exc:`TypeError` will be raised if there are any non-string values in
*iterable*, including :class:`bytes` objects::
*iterable*, including :class:`bytes` objects::

>>> ', '.join(['spam', 'spam', 'spam'])
'spam, spam, spam'
>>> ' & '.join(['spam', 'spam', 'spam'])
'spam & spam & spam'
>>> '-'.join('Python')
'P-y-t-h-o-n'

Expand All @@ -2128,14 +2134,14 @@
.. method:: str.ljust(width[, fillchar])

Return the string left justified in a string of length *width*. Padding is
done using the specified *fillchar* (default is an ASCII space). The
original string is returned if *width* is less than or equal to ``len(s)``::
done using the specified *fillchar* (default is an ASCII space) ::

>>> 'Python'.ljust(10)
'Python '
>>> 'Python'.ljust(10, '.')
'Python....'

The

Check warning on line 2143 in Doc/library/stdtypes.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Explicit markup ends without a blank line; unexpected unindent.
original string is returned if *width* is less than or equal to ``len(s)``

Check warning on line 2144 in Doc/library/stdtypes.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: rjust
See also :meth:`rjust`.

.. method:: str.lower()
Expand Down Expand Up @@ -2180,7 +2186,7 @@
ordinals (integers) or characters (strings of length 1) to Unicode ordinals,
strings (of arbitrary lengths) or ``None``. Character keys will then be
converted to ordinals::

.. remove examples , too esoteric

Check warning on line 2189 in Doc/library/stdtypes.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Literal block expected; none found.
>>> str.maketrans({'a': 'A', 'b': 'Boo', 'c': None})
{97: 'A', 98: 'Boo', 99: None}

Expand Down Expand Up @@ -2257,8 +2263,8 @@
that *sub* is contained within ``s[start:end]``. Optional arguments *start*
and *end* are interpreted as in slice notation::

>>> 'spam, spam, spam'.rfind('sp')
12
>>> 'spam, spam, spam'.rfind(',')
10
>>> 'spam, spam, spam'.rfind('sp', 0, 10)
6

Expand All @@ -2283,7 +2289,7 @@
Return the string right justified in a string of length *width*. Padding is
done using the specified *fillchar* (default is an ASCII space)::

>>> 'Python'.rjust(10)

Check warning on line 2292 in Doc/library/stdtypes.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

Lexing literal_block " >>> 'Python'.rjust(10)\n ' Python'\n >>> 'Python'.rjust(10, '.')\n '....Python'\n\nThe original string is returned if *width* is less than or equal to ``len(s)``." as "python3" resulted in an error at token: '`'. Retrying in relaxed mode.
' Python'
>>> 'Python'.rjust(10, '.')
'....Python'
Expand Down Expand Up @@ -2474,8 +2480,8 @@

With optional *end*, stop comparing string at that position::

>>> 'Python is amazing, yet not mysterious'.startswith('ama', 10, 15)
True
>>> 'Python is amazing, yet not mysterious'.startswith('ama', 10, 12)
False

See also :meth:`endswith` and :meth:`removeprefix`.

Expand Down Expand Up @@ -2569,16 +2575,18 @@
.. method:: str.upper()

Return a copy of the string with all the cased characters [4]_ converted to
uppercase. Note that ``s.upper().isupper()`` might be ``False`` if ``s``
uppercase::

>>> 'Monty Python'.upper()
'MONTY PYTHON'

Note that ``s.upper().isupper()`` might be ``False`` if ``s``
contains uncased characters or if the Unicode category of the resulting
character(s) is not "Lu" (Letter, uppercase), but e.g. "Lt" (Letter,
titlecase).
titlecase)::

::

>>> 'Monty Python'.upper()
'MONTY PYTHON'
>>> '𐊠'.upper().isupper() # 'CARIAN LETTER A'
>>> ' '.upper().isupper() # spaces
False

The uppercasing algorithm used is
Expand Down
Loading