# Subtract from a string (str -= x) as shortcut for str.replace(x, "")

**URL:** <https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439>\
**Category:** Python Help\
**Tags:** builtins\
**Created:** [July 28, 2023, 11:18am UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439 "2023-07-28T11:18:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cats](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cats/32/13742_2.png) [@Cats](https://discuss.python.org/u/Cats)\
**Post date:** [July 28, 2023, 11:18am UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/1 "2023-07-28T11:18:21Z")

</div>

Fun thought. We have string concatenation, so what about the opposite?

I was brainstorming how `str -= x` could work, and considering the purpose of `.[l|r]strip(x)` which would handle removing characters from any/either side, I think the syntax should equal the same as `str.replace(x, '')`, as there’s currently no shortcut for it. I would accept either the exact same, or kind of like `strip(x)` where `x` is interpreted as a list of characters and not a full match.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 28, 2023, 11:22am UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/2 "2023-07-28T11:22:56Z")

</div>

I’m sure this has been suggested many times. There are no clear semantics for such an operator, and also no clear use case. If you have to debate what it should do, then code that uses it will necessarily be unclear (the person using it might have not have been on your side in the debate).

If I had to redesign everything from scratch, I would probably use a different operator than `+` for string concatenation.

---

<div class="post-metadata">

**Author:** ![Cats](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cats/32/13742_2.png) [@Cats](https://discuss.python.org/u/Cats)\
**Post date:** [July 28, 2023, 11:53am UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/3 "2023-07-28T11:53:30Z")

</div>

@davidism Why was this moved to **Python Help**? I’m not requesting help in this post.

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [July 28, 2023, 11:58am UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/4 "2023-07-28T11:58:25Z")

</div>

As it stands, this is not written as a viable proposal to alter a standard library type. It’s a “fun thought” and “what about”, and does not take into account prior discussions, so it seems like you need help learning about and fleshing out the idea before proposing it.

---

<div class="post-metadata">

**Author:** ![Cats](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cats/32/13742_2.png) [@Cats](https://discuss.python.org/u/Cats)\
**Post date:** [July 28, 2023, 12:07pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/5 "2023-07-28T12:07:22Z")

</div>

I feel you’re reading into my wording too much, which are **purely** for the purpose of padding and not appearing like a close minded suggestion. I feel like good suggestions are open ended and don’t have only 1 possible outcome. My use of “fun thought” was simply me not trying to sound too cold. In the end the idea is fleshed out to me. I’d appreciate if this could be put back into the Ideas forum.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [July 28, 2023, 12:43pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/6 "2023-07-28T12:43:17Z")

</div>

Prior art exists. In Pike, strings support a number of arithmetic operators, including subtraction.

```auto
"hello, " + "world" == "hello, world"
"hello, world" - "l" == "heo, word"
"-=" * 10 + "-" == "-=-=-=-=-=-=-=-=-=-=-"
"this message has five words" / " " == array of five words
"split this into sections" / 8 == array of eight-character strings

```

While it’s true that subtraction could be considered equivalent to suffix removal, I think there’s good reason to consider it equivalent to replacement.

That said, though, there isn’t a lot of need for this in Python. Both “replace with nothing” and “remove suffix” can be done with method calls. There’s not all that much value to adding another way of doing the same thing.

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [July 28, 2023, 3:21pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/7 "2023-07-28T15:21:48Z")

</div>

Hello, @Cats, and welcome to the Python Discourse community!

Thanks for the fun thought. Yes, it could be fun and instructive to consider what a `-=` operator could do if it were offered for the `str` type. However, in my opinion, we should not actually proceed with implementing such an operator for that purpose in Python. The existing [`str.replace(old, new[, count])`](https://docs.python.org/3/library/stdtypes.html#str.replace) method is more useful in that it offers an optional `count` parameter, and therefore it would be best to continue to promote that method as the conventional and preferred means of performing the operation.

We hope you continue to enjoy the discussions here!

---

<div class="post-metadata">

**Author:** ![rhettinger](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rhettinger/32/1129_2.png) [@rhettinger](https://discuss.python.org/u/rhettinger)\
**Post date:** [July 28, 2023, 3:48pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/8 "2023-07-28T15:48:28Z")

</div>

> [@kknechtel](#):
>
> If I had to redesign everything from scratch, I would probably use a different operator than `+` for string concatenation.

I suspect that isn’t a widely held opinion ;-). Most of Guido’s design decisions have stood the test of time. When he gives talks about regrets, I don’t think this isn’t among them.

---

<div class="post-metadata">

**Author:** ![Zeturic](https://avatars.discourse-cdn.com/v4/letter/z/ecb155/32.png) [@Zeturic](https://discuss.python.org/u/Zeturic)\
**Post date:** [July 28, 2023, 6:03pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/9 "2023-07-28T18:03:04Z")

</div>

I suspect your proposal will get a lot of pushback.

Also, for what it’s worth, if I had to define `-` on `str`, my first choice would that `string - otherstring` should mean `string.removesuffix(otherstring)`, because that’s nearly an inverse for concatenation (`+`). To be clear, though, I’m not actually proposing that be added.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [July 28, 2023, 8:04pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/10 "2023-07-28T20:04:22Z")

</div>

Oh, I’m sure. The idiom is very popular across many languages. Still, there are others that dissent, and they have good reasons.

---

<div class="post-metadata">

**Author:** ![e-dreyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/e-dreyer/32/14055_2.png) [@e-dreyer](https://discuss.python.org/u/e-dreyer)\
**Post date:** [August 4, 2023, 6:10pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/11 "2023-08-04T18:10:10Z")

</div>

Your suggested behaviour isn’t predictable and formally defined.

For irony’s sake, let’s take the word “lol”.

Is “lol” -= “ol” or “lo” or “o” or does it raise a NoUniqueSolution exception?

---

<div class="post-metadata">

**Author:** ![e-dreyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/e-dreyer/32/14055_2.png) [@e-dreyer](https://discuss.python.org/u/e-dreyer)\
**Post date:** [August 4, 2023, 6:15pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/12 "2023-08-04T18:15:17Z")

</div>

People confuse additoin and concatenation. This is due to the similar use of the + uperator. 5 + 5 = 10, is addition. 5 + 5= 55 is concatenation.

So is 55 - 5 = _5 or 55 - 5 = 5_? Which 5 is removed?

3 + 2 = 5 (additiond)  
5 - 2 = 3 (subtraction)  
3 + 2 = 5 (concatenation)  
5 - 2 = ? (De-concatenation)  
5 - 3 = ? (De-concatenation)

There is a probleme here

---

<div class="post-metadata">

**Author:** ![fungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/fungi/32/5963_2.png) [@fungi](https://discuss.python.org/u/fungi)\
**Post date:** [August 4, 2023, 6:37pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/13 "2023-08-04T18:37:45Z")

</div>

And here I was expecting:

```auto
3 + 2 = 32 (concatenation)

```

Or maybe that’s what you meant?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [August 4, 2023, 6:52pm UTC](https://discuss.python.org/t/subtract-from-a-string-str-x-as-shortcut-for-str-replace-x/30439/14 "2023-08-04T18:52:10Z")

</div>

> [@e-dreyer](#):
>
> For irony’s sake, let’s take the word “lol”.
> 
> Is “lol” -= “ol” or “lo” or “o” or does it raise a NoUniqueSolution exception?

```auto
"lol" - "ol" == "l"
"lol" - "l" = "o"

```

Semantics are pretty straight-forward. Alternatively, and equally sanely, you could define that “lol” - “l” = “lo”, but IMO that’s less useful. Either way, though, something like this simply gets a well-defined semantic meaning, and you follow that. It’s not hard for it to be well-defined; it’s hard for it to be sufficiently useful to justify being an operator.
