Skip to content

Commit

Permalink
gh-106487: Allow the 'count' argument of str.replace to be a keyword (
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jul 10, 2023
1 parent dac1e36 commit 34c1414
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
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

0 comments on commit 34c1414

Please sign in to comment.