# Feature Proposal: Multi-String Replacement Using a Dictionary in the .replace() Method

**URL:** <https://discuss.python.org/t/feature-proposal-multi-string-replacement-using-a-dictionary-in-the-replace-method/68609>\
**Category:** Ideas\
**Created:** [October 21, 2024, 12:13am UTC](https://discuss.python.org/t/feature-proposal-multi-string-replacement-using-a-dictionary-in-the-replace-method/68609 "2024-10-21T00:13:38Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [October 23, 2024, 8:29pm UTC](https://discuss.python.org/t/feature-proposal-multi-string-replacement-using-a-dictionary-in-the-replace-method/68609/21 "2024-10-23T20:29:28Z")

</div>

This is closely related to string improvements myself and @Nineteendo were working on.

I don’t think doing a naive:

```python
s.replace(...).replace(...)

```

is the best way to go. Instead, efficient version (which allows swapping) is possible and is a natural consequence of: [tuple find](https://discuss.python.org/t/add-tuple-support-to-more-str-functions/50628/136)

Ideally, this will be easy and natural to implement after:  
a) [gh-119702: New dynamic algorithm selection for string search (+ `rfind` alignment) by dg-pb · Pull Request #120025 · python/cpython · GitHub](https://github.com/python/cpython/pull/120025)  
b) [Add tuple support to more str functions - #136 by dg-pb](https://discuss.python.org/t/add-tuple-support-to-more-str-functions/50628/136)  
c) [String API changes](https://discuss.python.org/t/string-api-changes/62986)

After these, implementing multi-pattern `replace`, `split` and similar will be straight forward.

I think it would be good to keep API consistent. And currently it seems that string methods tend to use `tuple` arguments. Such as `str.endswith` and upcomming `str.find`.

And I think it would work well, as:

1. `tuple` are much less complex objects than `dict` and there is no real need for `dict`
2. It would provide extra convenience replacing many substrings with a single one (see below)

Keeping `old` and `new` for this would look like:

```python
string = 'a_b_c'
string.replace(('a', 'b'), ('z', 'z')) # 'z_z_c'
string.replace(('a', 'b'), 'z') # 'z_z_c'
string.replace(('a', 'b'), ('b', 'a')) # 'b_a_c'

```

If someone prefers dictionary, then one can simply do:

```python
d = {'a': 'b', 'b': 'a'}
string.replace(tuple(d.keys()), tuple(d.values()))

```

Or even implement it in C if that is highly desirable.

**The whole work has been stuck on [gh-119702: New dynamic algorithm selection for string search (+ `rfind` alignment) by dg-pb · Pull Request #120025 · python/cpython · GitHub](https://github.com/python/cpython/pull/120025) for quite a while now. So if someone would like to help move things forward here, next step is coredev review.**

---

_[View the full topic](https://discuss.python.org/t/feature-proposal-multi-string-replacement-using-a-dictionary-in-the-replace-method/68609)._
