Skip to content

Commit e7cea78

Browse files
[3.12] Update example of str.split, bytes.split (GH-121287) (#121416)
Update example of str.split, bytes.split (GH-121287) In `{str,bytes}.strip(chars)`, multiple characters are not treated as a prefix/suffix, but as individual characters. This may make users confuse whether `split` has similar behavior. Users may incorrectly expect that `'Good morning, John.'.split(', .') == ['Good', 'morning', 'John']` Adding a bit of clarification in the doc. (cherry picked from commit 892e3a1) Co-authored-by: Yuxin Wu <[email protected]> Co-authored-by: Yuxin Wu <[email protected]>
1 parent 9e542f9 commit e7cea78

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Doc/library/stdtypes.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,8 +2092,9 @@ expression support in the :mod:`re` module).
20922092
If *sep* is given, consecutive delimiters are not grouped together and are
20932093
deemed to delimit empty strings (for example, ``'1,,2'.split(',')`` returns
20942094
``['1', '', '2']``). The *sep* argument may consist of multiple characters
2095-
(for example, ``'1<>2<>3'.split('<>')`` returns ``['1', '2', '3']``).
2096-
Splitting an empty string with a specified separator returns ``['']``.
2095+
as a single delimiter (to split with multiple delimiters, use
2096+
:func:`re.split`). Splitting an empty string with a specified separator
2097+
returns ``['']``.
20972098

20982099
For example::
20992100

@@ -2103,6 +2104,8 @@ expression support in the :mod:`re` module).
21032104
['1', '2,3']
21042105
>>> '1,2,,3,'.split(',')
21052106
['1', '2', '', '3', '']
2107+
>>> '1<>2<>3<4'.split('<>')
2108+
['1', '2', '3<4']
21062109

21072110
If *sep* is not specified or is ``None``, a different splitting algorithm is
21082111
applied: runs of consecutive whitespace are regarded as a single separator,
@@ -3140,10 +3143,9 @@ produce new objects.
31403143
If *sep* is given, consecutive delimiters are not grouped together and are
31413144
deemed to delimit empty subsequences (for example, ``b'1,,2'.split(b',')``
31423145
returns ``[b'1', b'', b'2']``). The *sep* argument may consist of a
3143-
multibyte sequence (for example, ``b'1<>2<>3'.split(b'<>')`` returns
3144-
``[b'1', b'2', b'3']``). Splitting an empty sequence with a specified
3145-
separator returns ``[b'']`` or ``[bytearray(b'')]`` depending on the type
3146-
of object being split. The *sep* argument may be any
3146+
multibyte sequence as a single delimiter. Splitting an empty sequence with
3147+
a specified separator returns ``[b'']`` or ``[bytearray(b'')]`` depending
3148+
on the type of object being split. The *sep* argument may be any
31473149
:term:`bytes-like object`.
31483150

31493151
For example::
@@ -3154,6 +3156,8 @@ produce new objects.
31543156
[b'1', b'2,3']
31553157
>>> b'1,2,,3,'.split(b',')
31563158
[b'1', b'2', b'', b'3', b'']
3159+
>>> b'1<>2<>3<4'.split(b'<>')
3160+
[b'1', b'2', b'3<4']
31573161

31583162
If *sep* is not specified or is ``None``, a different splitting algorithm
31593163
is applied: runs of consecutive ASCII whitespace are regarded as a single

0 commit comments

Comments
 (0)