Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2014,11 +2014,14 @@ expression support in the :mod:`re` module).
.. versionadded:: 3.9


.. method:: str.replace(old, new[, count])
.. method:: str.replace(old, new, count=-1)

Return a copy of the string with all occurrences of substring *old* replaced by
*new*. If the optional argument *count* is given, only the first *count*
occurrences are replaced.
*new*. If *count* is given, only the first *count* occurrences are replaced.
If *count* is not specified or ``-1``, then all occurrences are replaced.

.. versionchanged:: 3.13
*count* is now supported as a keyword argument.


.. method:: str.rfind(sub[, start[, end]])
Expand Down
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ New Features
Other Language Changes
======================


* Allow the *count* argument of :meth:`str.replace` to be a keyword.
(Contributed by Hugo van Kemenade in :gh:`106487`.)

New Modules
===========
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ def test_count(self):
self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))

def test_count_keyword(self):
self.assertEqual('aa'.replace('a', 'b', 0), 'aa'.replace('a', 'b', count=0))
self.assertEqual('aa'.replace('a', 'b', 1), 'aa'.replace('a', 'b', count=1))
self.assertEqual('aa'.replace('a', 'b', 2), 'aa'.replace('a', 'b', count=2))
self.assertEqual('aa'.replace('a', 'b', 3), 'aa'.replace('a', 'b', count=3))

def test_find(self):
self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow the *count* argument of :meth:`str.replace` to be a keyword. Patch by
Hugo van Kemenade.
44 changes: 36 additions & 8 deletions Objects/clinic/unicodeobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -12025,10 +12025,10 @@ str.replace as unicode_replace

old: unicode
new: unicode
/
count: Py_ssize_t = -1
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
/

Return a copy with all occurrences of substring old replaced by new.

Expand All @@ -12039,7 +12039,7 @@ replaced.
static PyObject *
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
Py_ssize_t count)
/*[clinic end generated code: output=b63f1a8b5eebf448 input=147d12206276ebeb]*/
/*[clinic end generated code: output=b63f1a8b5eebf448 input=3345c455d60a5499]*/
{
return replace(self, old, new, count);
}
Expand Down