Python String removesuffix() Method



The Python string removesuffix() method is used to remove a specified suffix from the end of a string.

If the string ends with the specified suffix, the method removes the suffix from the string and returns the modified string. If the string does not end with the specified suffix, the method returns the original string unchanged.

Syntax

Following is the basic syntax of the Python String removesuffix() method −

string.removesuffix(suffix)

Parameter

This method accepts a string as a parameter that specifies the suffix to be removed from the end of the string.

Return Value

The method returns a new string with the specified suffix removed from the end.

Example

In the following example, we are removing the suffix ".jpg" from the string "text" using the removesuffix() method −

text = "Hello World.jpg"
result = text.removesuffix(".jpg")
print(result)   

Output

The output obtained is as follows −

Hello World

Example

This example shows that if the given suffix does not exist in the specified string, the original string is returned without any modifications −

text = "Hello World"
result = text.removesuffix(".jpg")
print(result)    

Output

Following is the output of the above code −

Hello World

Example

The removesuffix() method, by default, performs a case-sensitive removal. In this instance, the method does not remove the suffix ".jpg" from the string "text" and returns the original string instead −

text = "Hello World.JPG"
result = text.removesuffix(".jpg")
print(result)

Output

The result produced is as shown below −

Hello World.JPG

Example

Now, we are removing the numeric suffix "12345" from the string "text" −

text = "Hello World12345"
result = text.removesuffix("12345")
print(result) 

Output

We get the output as shown below −

Hello World
split_and_join.htm
Advertisements