Python Program to remove the last specified character from the string


Text data manipulation and processing can benefit from using a Python program that will eliminate the last specified character from a string. This kind of application can be used to modify data by deleting particular characters, validate user input by removing incorrect characters, and clean up the text by removing unwanted characters. In Python, we have some string in-built functions like rstrip() that remove the last specified character from the string. The slicing technique is the easier way to remove the character from the end.

Syntax

The following syntax is used in the examples −

len()

The len() is an in-built function used to find the length of the string in Python.

rstrip() 

The rstrip() is an in-built function that accepts the parameter to remove the character.

[:-1]

The above representation is known for slicing the character from the end. The integer 1 depicts that it will remove the last character.

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.

endswidth()

This is an in-built method used in Python and if the string ends with the given value it returns true, otherwise false.

Example 1

In the following example, we will start the program by storing the input string in the variable inp_str. Then create the empty string variable remove_last_char that will later store the string by removing the last specified character. Now we are using the for loop and do the following &miinus;

len(inp_str)-1 − The loop range starts from the 0th index and iterates till the second last character because of -1. Finally, we are printing the variable with the help of the variable.

inp_str = "Tutorialspoints"
remove_last_char = ""
for i in range( len(inp_str)-1 ):
   remove_last_char += inp_str[i]
print("The updated string is:\n",remove_last_char)

Output

The updated string is:
Tutorialspoint

Example 2

In the following example, we will start the program by initializing the variable named my_str and storing the value of the input string. Then remove the last character of the string by using the in-built function named rstrip() and store it in variable trim_last_char. Finally, print the result with the help of the variable trim_last_char.

my_str = "ROCK AND ROLLS"
trim_last_char = my_str.rstrip('S')
print("After removing the last character", trim_last_char)

Output

After removing the last character ROCK AND ROLL

Example 3

In the following example, we will start the program by storing the input string variable named is_str. Then initialize the variable mod_str to store the value by removing the last character. is_str[:-1]: -1 represents the string in reverse mode, ‘:’ slicing one character from the end. Finally, we are printing the variable with the help of the variable mod_str.

is_str = "INTERPRETERS"
mod_str = is_str[:-1]
print("After removing the last character", mod_str)

Output

After removing the last character INTERPRETER

Example 4

In the following example, we will first store the input string character in the variable str_name. Then store the last specified character in the variable last_suffix. Then use the if-statement to check the condition for last specified character using endswith(). Next, use the replace() method with given string that will replace the last character and store it in the variable str_name. Finally, get the result with the help of the variable str_name.

str_name = "abcdefghi"
last_suffix = "i"
if str_name.endswith(last_suffix):
   str_name = str_name.replace(last_suffix, "")
print("After deleting the last specified character from the given string:",str_name)

Output

After deleting the last specified character from the given string: abcdefgh

Conclusion

The above three outputs represented the string by removing the character from the end of the string. We explored how slice, len(), and rstrip() were helpful in removing the character.

Updated on: 01-Jun-2023

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements