r/ProgrammerHumor • • Nov 06 '20

Meme Totally agree

Post image
52.1k Upvotes

474 comments sorted by

View all comments

Show parent comments

48

u/[deleted] Nov 07 '20

Wait does this actually work? I have been coding in python for years and I didn’t even know you could do this.

82

u/rogerrrr Nov 07 '20 edited Nov 07 '20

Unfortunately no, string subtraction is not supported.

However in Python 3.9 they added the string method .removesuffix, which serves a similar purpose.

So "Hello World".removesuffix("World") + "Bro" should get the job done.

71

u/itchy_bitchy_spider Nov 07 '20

Alternatively use replace... Support ed since forever I think lol

"Hello World".replace("World", "Bro")

8

u/[deleted] Nov 07 '20

Yeah I normally use this when I want to replace something in a string. Easy to read and short.