8000 gh-106318: Add string method examples by blaisep · Pull Request #119445 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106318: Add string method examples #119445

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

Draft
wants to merge 67 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adjust the placement of examples more meticulously.
  • Loading branch information
blaisep committed May 28, 2024
commit 4d70dcfc0a0925fa6c7d7b5111270bdcf8fa2913
60 changes: 35 additions & 25 deletions Doc/library/stdtypes.rst
< 8000 td id="diff-08a31a70dd1f6d97aa8dacdce77db4de04c700d9949be1af611a595186aad5b3R2001" data-line-number="2001" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -2001,18 +2001,25 @@ expression support in the :mod:`re` module).

Return ``True`` if all characters in the string are numeric
characters, and there is at least one character, ``False``
otherwise. Numeric characters include digit characters, and all characters
that have the Unicode numeric value property, e.g. U+2155,
VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property
value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
otherwise::

::
>>> '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::

>>> '٠١٢٣٤٥٦٧٨٩'.isnumeric() # ARABIC-INDIC DIGIT ZERO to NINE
True
>>> '⅕'.isnumeric() # VULGAR FRACTION ONE FIFTH
True


Formally, numeric characters are those with the property
value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric::

>>> '²'.isdigit(), '²'.isdecimal(), '²'.isnumeric()
(True, False, True)

Expand All @@ -2023,15 +2030,18 @@ expression support in the :mod:`re` module).
.. method:: str.isprintable()

Return ``True`` if all characters in the string are printable or the string is
empty, ``False`` otherwise. Nonprintable characters are those characters defined
in the Unicode character database as "Other" or "Separator", excepting the
ASCII space (0x20) which is considered printable.
empty, ``False`` otherwise::

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

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


>>> '\t\n'.isprintable() # TAB and BREAK LINE
False
>>> '\u3000'.isprintable() # IDEOGRAPHIC SPACE
Expand Down Expand Up @@ -2338,31 +2348,30 @@ If the separator is not found, return a 3-tuple containing
string. If *maxsplit* is given, at most *maxsplit* splits are done (thus,
the list will have at most ``maxsplit+1`` elements). If *maxsplit* is not
specified or ``-1``, then there is no limit on the number of splits
(all possible splits are made).

If *sep* is given, consecutive delimiters are not grouped together and are
deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
``['1', '', '2']``). The *sep* argument may consist of multiple characters
(for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Splitting an empty string with a specified separator returns ``['']``.

For example::
(all possible splits are made)::

>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']


If *sep* is given, consecutive delimiters are not grouped together and are
deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
``['1', '', '2']``)::

>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']


The *sep* argument may consist of multiple characters
(for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
Splitting an empty string with a specified separator returns ``['']``.

If *sep* is not specified or is ``None``, a different splitting algorithm is
applied: runs of consecutive whitespace are regarded as a single separator,
and the result will contain no empty strings at the start or end if the
string has leading or trailing whitespace. Consequently, splitting an empty
string or a string consisting of just whitespace with a ``None`` separator
returns ``[]``.

For example::
string has leading or trailing whitespace::

>>> '1 2 3'.split()
['1', '2', '3']
Expand All @@ -2371,6 +2380,9 @@ If the separator is not found, return a 3-tuple containing
>>> ' 1 2 3 '.split()
['1', '2', '3']

Consequently, splitting an empty string or a string consisting of just whitespace with a ``None`` separator
returns ``[]``.

See also :meth:`rsplit`.

.. index::
Expand Down Expand Up @@ -2590,8 +2602,6 @@ If the separator is not found, return a 3-tuple containing
>>> "-42".zfill(5)
'-0042'



.. _old-string-formatting:

``printf``-style String Formatting
Expand Down
0