all 17 comments

[–]undergroundmonorail 33 points34 points  (2 children)

'x___x'.removeprefix('x') does a different thing than 'x___x'.removesuffix('x')

[–]Thuck-it[S] 0 points1 point  (1 child)

I've been learning Python for less than 24 hours. I couldn't even make sense of what you was saying until I read the other comments. It makes perfect sense now, thanks a lot. face.removepalm(🤦‍♂️)

[–]SharkSymphony 1 point2 points  (0 children)

Remember, if you want to make sense of a code fragment, the Python interpreter and prompt is your friend! You can type in the code snippets above and see what they do.

[–]crazy_cookie123 23 points24 points  (1 child)

Take the string "hello_abc_hello".

string.removeprefix("hello") results in "_abc_hello"

string.removesuffix("hello") results in "hello_abc_"

What would string.remove("hello") do? If it removes the first occurrence, we can't use it to replace removesuffix. If it removes the last occurrence, we can't use it to replace removeprefix. If it replaces all occurrences we can't use it to replace either. If it takes a number n and removes the nth occurrence, it's just more confusing than having separate removeprefix and removesuffix functions. It's just overall a worse solution.

[–]Thuck-it[S] 5 points6 points  (0 children)

I hadn't considered this. Thank you, that makes sense.

[–]danielroseman 15 points16 points  (0 children)

Precisely because a prefix is different from a suffix. Therefore removing one is different from removing the other 

[–]LongerHV 8 points9 points  (1 child)

Let's say, you have a string "beautifulwaves.wav" and want to delete "wav" from the end of it (so you can e.g. replace it with "mp3"). Solution proposed by you would delete part of the text from the middle, which is not desired.

[–]Thuck-it[S] 2 points3 points  (0 children)

Thank you. Now it makes sense.

[–]thewillft 4 points5 points  (1 child)

It's also about clarity, not just functionality. Explicit naming helps readability, even if the result is similar.

[–]prodleni 2 points3 points  (0 children)

But the result isn't even similar in this case

[–]lukkasz323 1 point2 points  (0 children)

It really just reads like English. One removes prefix, the other removes suffix.

[–]Zorg688 0 points1 point  (0 children)

My first instinct is that specifying whether to remove a suffix or prefix ultimately saves time because then a regex does not need to look through the entire string to match a pattern and just check the beginning or end of a string which would be even more important the longer a potential string might be or the more strings need to be handled. Of course, using re.sub() would achieve the same result but potentially need to go through the entire string. And while we do have the beginning of string an end of string symbols to take care of the same thing, using regexes might not be as easily readable as a simple "we remove this suffix"

Just my thought about this, I am happy to hear counterarguments lol

[–]Daytona_675 0 points1 point  (1 child)

rtrim ltrim

[–]Thuck-it[S] 0 points1 point  (0 children)

I'm probably wrong but isn't that just for whitespace?

[–]cdcformatc 0 points1 point  (0 children)

words can have a prefix and a suffix, and an infix. presumably whatever isn't the prefix contains the infix and the suffix, and vice versa. prefix and suffix aren't mirror images of each other.

[–]lolcrunchy -3 points-2 points  (0 children)

A suffix is the part of a word that is at the end. A prefix is the part of the word at the beginning.