I wish #Python's string replace method allowed specifying "count" as a keyword argument.

>>> text = "Hello? Python?"
>>> text.replace("?", "!", count=1)

I find that more explicit than this:

>>> text.replace("?", "!", 1)
'Hello! Python?'

But the replace only allows positional arguments 馃悕馃槩

@treyhunner Make a GitHub issue and even a PR?
@adamchainz A Python ideas thread was suggested too, but it looks like @hugovk just opened an issue 馃槉: https://github.com/python/cpython/issues/106487
Allow `str.replace`'s `count` to be a keyword argument 路 Issue #106487 路 python/cpython

Feature or enhancement (A clear and concise description of your proposal.) Pitch str.replace takes old and new strings as parameters, and an optional count parameter: https://docs.python.org/3/libr...

GitHub
gh-106487: Allow the *count* argument of `str.replace` to be a keyword by hugovk 路 Pull Request #106488 路 python/cpython

Before >>> "aa".replace("a", "b") 'bb' >>> "aa".replace("a", "b", 1) 'aa' >>> "aa".replace("a", "b", count=1) TypeError: str.replace() takes no keyword arguments After >>> "aa".replace("a", "b") 'b...

GitHub
@hugovk @treyhunner MCDD (Mastodon Complaint Driven Development) at its finest

@adamchainz @treyhunner And https://github.com/python/cpython/pull/106488 is merged!

Keyword `count` in `str.replace` is coming to a Python* near you soon**!

* 3.13
** October 2024

https://devguide.python.org/versions/

gh-106487: Allow the *count* argument of `str.replace` to be a keyword by hugovk 路 Pull Request #106488 路 python/cpython

Before >>> "aa".replace("a", "b") 'bb' >>> "aa".replace("a", "b", 1) 'aa' >>> "aa".replace("a", "b", count=1) TypeError: str.replace() takes no keyword arguments After >>> "aa".replace("a", "b") 'b...

GitHub

@adamchainz @treyhunner And https://github.com/python/cpython/pull/147943 is merged!

Keyword `count` in `bytes.replace` is coming to a Python* near you soon**!

* 3.15
** October 2026

#Python #python315

gh-147856: Allow the 'count' argument of `bytes.replace()` to be a keyword by StanFromIreland 路 Pull Request #147943 路 python/cpython

Borrowed the wording from #106488 :-) Issue: Allow bytes.replace's 'count' to be a keyword argument #147856 馃摎 Documentation preview 馃摎: https://cpython-previews--147943.org.readth...

GitHub