Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit e89173f

Browse files
Update removesuffix.md
1 parent 6f9c0df commit e89173f

1 file changed

Lines changed: 37 additions & 42 deletions

File tree

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
Title: '.roundsuffix()'
3-
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.'
44
Subjects:
5-
- 'Python'
5+
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
88
- 'Methods'
@@ -11,72 +11,67 @@ CatalogContent:
1111
- 'learn-python-3'
1212
- 'paths/computer-science'
1313
---
14-
# removesuffix()
1514

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 case sensitive.
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.
1716

1817
## Syntax
1918

20-
```py
19+
```pseudo
2120
string_name.removesuffix(suffix)
2221
```
23-
**string_name** is the string's variable name and **suffix** is the specified part you want to remove.
2422

25-
## Example
23+
**Parameters:**
2624

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

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
3132

32-
file_type.removesuffix('docx')
33+
This method can be used to remove a file extension when the suffix matches exactly:
3334

34-
print(file_type)
35+
```py
36+
file_type = "Cat Store.docx"
37+
result = file_type.removesuffix('.docx')
38+
print(result)
3539
```
40+
3641
The output would look like this:
37-
```
42+
43+
```shell
3844
Cat Store
3945
```
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).'
4346

44-
quote.removesuffix('there is no try')
47+
## Example 2: Case sensitivity
4548

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:
5450

5551
```py
5652
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)
6255
```
56+
6357
Here's the output:
64-
```
58+
59+
```shell
6560
And when I silently snuck up on my mom, I jumped up and exclaimed 'BOO!'
6661
```
67-
## Codebyte
6862

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)"
63+
## Example 3: Removing text from a sentence
7264

73-
quote1 = famoussaying1.removesuffix("Aslan")
74-
quote2 = famoussaying2.removesuffix("I loved you!")
65+
The suffix must match the exact ending of the string, including punctuation:
7566

76-
print(quote1)
77-
print(quote2)
67+
```py
68+
quote = 'Do or do not, there is no try (Yoda).'
69+
result = quote.removesuffix(' (Yoda).')
70+
print(result)
7871
```
7972

73+
The output for this code would look like this:
8074

81-
82-
75+
```shell
76+
Do or do not, there is no try
77+
```

0 commit comments

Comments
 (0)