You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 27, 2026. It is now read-only.
Description: 'Removes a specific suffix from a string'
2
+
Title: '.removesuffix()'
3
+
Description: 'Returns a copy of a string with the specified suffix removed, if present.'
4
4
Subjects:
5
-
- 'Python'
5
+
- 'Code Foundations'
6
6
- 'Computer Science'
7
7
Tags:
8
8
- 'Methods'
@@ -11,72 +11,67 @@ CatalogContent:
11
11
- 'learn-python-3'
12
12
- 'paths/computer-science'
13
13
---
14
-
# removesuffix()
15
14
16
-
**removesuffix()**is a Python method that removes a specified suffix from a string. This method is helpful when you want to remove any unwanted parts of your string. It is casesensitive.
15
+
The**`str.removesuffix()`**method returns a new string with the specified suffix removed, if present. If the string does not end with the given suffix, the original string is returned unchanged. This method is case-sensitive and does not modify the original string.
17
16
18
17
## Syntax
19
18
20
-
```py
19
+
```pseudo
21
20
string_name.removesuffix(suffix)
22
21
```
23
-
**string_name** is the string's variable name and **suffix** is the specified part you want to remove.
24
22
25
-
## Example
23
+
**Parameters:**
26
24
27
-
This method can be used for a string whose suffix is distinct and separated by punctuation like **(.)**.
25
+
-`suffix`: The string to remove from the end of the original string.
28
26
29
-
```py
30
-
file_type ="Cat Store.docx"
27
+
**Return value:**
28
+
29
+
A new string with the suffix removed if it exists, otherwise, the original string.
30
+
31
+
## Example 1: Removing a file extension
31
32
32
-
file_type.removesuffix('docx')
33
+
This method can be used to remove a file extension when the suffix matches exactly:
33
34
34
-
print(file_type)
35
+
```py
36
+
file_type ="Cat Store.docx"
37
+
result = file_type.removesuffix('.docx')
38
+
print(result)
35
39
```
40
+
36
41
The output would look like this:
37
-
```
42
+
43
+
```shell
38
44
Cat Store
39
45
```
40
-
The suffix can also be specified as the ending of a string.
41
-
```py
42
-
quote ='Do or do not, there is no try (Yoda).'
43
46
44
-
quote.removesuffix('there is no try')
47
+
## Example 2: Case sensitivity
45
48
46
-
print(quote)
47
-
```
48
-
The output for this code would look like this:
49
-
50
-
```
51
-
Do or do not, (Yoda).
52
-
```
53
-
Here's an example demonstrating that roundsuffix() is case-sensitive:
49
+
`.removesuffix()` is case-sensitive, so, if the case does not match exactly, the suffix is not removed:
54
50
55
51
```py
56
52
statement ="And when I silently snuck up on my mom, I jumped up and exclaimed 'BOO!'"
57
-
58
-
statement.roundsuffix('Boo!')
59
-
60
-
print(statement)
61
-
53
+
result = statement.removesuffix('Boo!')
54
+
print(result)
62
55
```
56
+
63
57
Here's the output:
64
-
```
58
+
59
+
```shell
65
60
And when I silently snuck up on my mom, I jumped up and exclaimed 'BOO!'
66
61
```
67
-
## Codebyte
68
62
69
-
```codebyte/python 3
70
-
famoussaying1 = "For Narnia and for Aslan!!! (Peter, The Lion, The Witch, and The Wardrobe)"
71
-
famoussaying2 = "You were my brother Anakin! I loved you! (Obi Wan, Revenge of the Sith)"
0 commit comments