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

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.

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.

1 Like

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

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.

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.

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

"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.

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]) 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!

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.

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.

2 Likes

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

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?

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

And here I was expecting:

3 + 2 = 32 (concatenation)

Or maybe that’s what you meant?

"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.

1 Like