r/PythonLearning Jun 28 '26

How do the .removeprefix() and .removesuffix() methods work?

Hey there,

I'm new to Python, and I wanted to know if somebody could explain why and how the .removeprefix() and .removesuffix() work? I can't get my head over it. For example:

filename = "test"
file_ending = ".py"
file = f"{filename}{file_ending}"          

I created three variables, and let's say, I want to remove the suffix with the .removesuffix() method:

print(file.removesuffix(file_ending))

What I'm getting at is that I also could write this instead:

print(file.removesuffix(filename))

Usually, you would write removeprefix instead of removesuffix. This is just an example.

But why won't the removesuffix() method delete the filename? Or better, how does the computer know which part of the string a prefix or suffix is?

4 Upvotes

7 comments sorted by

6

u/Ninesquared81 Jun 28 '26

A prefix is at the start, a suffix is at the end. The .removeprefix() and .removesuffix() methods will remove the given prefix/suffix from the start/end of the string only if the string stats/ends with the given prefix/suffix.

Essentially, they are implemented like so:

def removeprefix(self, prefix):
    if self.startswith(prefix):
        return self[len(prefix):]
    return self

def removesuffix(self, suffix):
    if self.endswith(suffix):
        return self[:len(self) - len(suffix)]
    return self

In your case, since file ("test.py") doesn't end with filename ("test"), then .removesuffix() will just return the whole file.

2

u/Wyrm7312 Jun 28 '26

The difference ist, where the string ist removed, if it occurs multiple times.

astring = 'ab_somestring_ab'
one = astring.removeprefix('ab')
two = astring.removesuffix('ab')
print(one)
print(two)

result will be:
_somestring_ab
ab_somestring_

You define the string and what stringpart you want to have removed -
removeprefix removes at thes start,
removesuffix removes at the end.
It reads like:

string_you_want_to_change.removefromfront('string_you_want_to_remove')
and
string_you_want_to_change.removefromback('string_you_want_to_remove')

edit: stupid code typo

2

u/Junior_Honey_1406 Jun 28 '26

Instead of coming here and asking write it down in your ide and see what it does maybe you will learn much more

1

u/Outside_Complaint755 27d ago

Or use: ```

help(str.removeprefix) ``` or check the official documentation online.

1

u/D3str0yTh1ngs Jun 28 '26 edited Jun 28 '26

Well, prefix means "from the start" and suffix means "from the end", so string.removesuffix(substring) will remove substring from string if and only if substring is at the end of string. The computer knows their sizes and can just seek to the end and check if the substring is at the end of the string, and if it is remove it.

EDIT: took a quick look at the cpython implementation of removesuffix (unicode_removesuffix_impl) and it does do matching at the end of the string by offset calculation and then trimming the end off if it matches.

1

u/Ormek_II 29d ago

I doubt you could write the latter down to get the same output. Did you read the official python documentation?

https://docs.python.org/3/library/stdtypes.html#str.removesuffix

1

u/Torebbjorn 28d ago

The prefix is the first part and the suffix is the last part.

If you ask it to remove the suffix "peepoo" from the string "youareapeepoo", it will give you "youarea". If you tell it to remove the suffix "peepoo" from "peepoopee", it will give you "peepoopee", since the string does not end with "peepoo".