
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete Suffix from Given String in Python
In Python, we have some built-in string functions like rstrip(), replace(), and endswith() that can be used to delete suffix from the given string. The delete suffix is termed by removing the group of characters at the end of the string. Sometimes we find errors like suffix in any data and if we want to delete it, it takes a lot of time and this can be solved by giving an algorithm to it. The end character or substring is termed a suffix.
Let's take an example to understand this ?
The given string variable is BRIGHTNING and after removing the suffix i.e. NING will make that word BRIGHT.
Syntax
endswith()
This is the predefined method used in Python and if the string ends with the given value it returns true, otherwise false.
rstrip()
This is the predefined method used in Python and removes the given suffix character from the end of the string.
replace("replace_string_var_name", "")
This is an in-built method in Python that accepts two parameters ?
replace_string_var_name ? Mention the name of the variable.
Empty string ? The empty string is represented by "" that stores the rest of the substring from the given string.
Algorithm
We are taking the input string variable.
Then we are using the if-statement to set the predefined method i.e. endswith with the input string variable to get the suffix substring. If the substring is found to be the end then it returns true, otherwise false. (Example 1 and Example 2)
Then we are using the predefined method i.e. rstrip with the input string variable. This method removes the string from the end. (Example 3)
Then use the if-statement to check the condition for removal of the substring using an endswith() function. (Example 4)
Moving ahead to calculate the length operation of the suffix substring and do the following ?
str_name = str_name[:-len("car")] ? Through this representation, we are depicting the minus to the length of the given suffix substring. (Example 1)
str_name = str_name[:len(str_name)-len(suffix_name)] ? Through this representation, we are subtracting the length of the string from the given string. (Example 2)
Next, use the replace method that replaces the specific given string and get the result. (Example 4)
Finally, we are printing the result with a help of an input string variable.
So, this way we will delete the suffix substring.
Example 1
In this program, we are using a predefined function i.e. endswith() to set the suffix string into it and delete the length of the suffix by denoting the -(minus) sign and get the result.
str_name = "RANSOMEWARE" if str_name.endswith("car"): str_name = str_name[ :-len("WARE") ] print( "After deleting the given suffix:", str_name )
Output
After deleting the given suffix: RANSOME
Example 2
In this program, we are taking two variables- input string and suffix string then using of predefined function i.e. endswith() it set the suffix string. To delete the suffix substring we are subtracting the length of a suffix variable from the total length of the given string variable.
str_name = "Doctorate" suffix_name = "ate" if str_name.endswith( suffix_name ): str_name = str_name[ :len(str_name)-len(suffix_name) ] print( "After deleting the given suffix:", str_name )
Output
After deleting the given suffix: Doctor
Example 3
In this program, we are using a predefined function i.e. rstrip() to delete the suffix substring from the given string and get the result.
str_name = "Rectangle" str_name = str_name.rstrip( "gle" ) print( "After deleting the given suffix:", str_name )
Output
After deleting the given suffix: Rectan
Example 4
In the following example, we will start storing the input string in the variable str_name. Then store the removal substring in the variable del_suffix. Then use the if-statement to check the condition for string removal using the built-in method endswith(). Next, use the replace method that accepts two parameters- del_suffix(to delete the substring) and ""(empty string to store the rest string). Then print the result with the help of the variable del_suffix.
str_name = "abcdefghi" del_suffix = "ghi" if str_name.endswith(del_suffix): str_name = str_name.replace(del_suffix, "") print("After delete the substring from the given string:",str_name)
Output
After deleting the substring from the given string: abcdef
Conclusion
We explored the concept to delete the suffix substring from the given string. We saw there are three examples of this program and the solving ways are different by taking the length operation and the predefined method. This type of program is normally used to delete multiple words ending with the same suffix.