Python Program to trim a string from the right side


In Python, we have a predefined function rstrip() to remove the character from the right side. It means it will remove the whitespace from the rightside side of the string.

Let’s take an example to understand the trim of a string from the left side.

  • In the given string “WIRELESS” remove the right string LESS and get the result value as “WIRE”.

  • In the given string “kingdom” remove the right string dom and get the result value as “king”.

Syntax

The following syntax used in the examples are −

isspace()

This is a predefined method used in Python to allow the whitespace, newline, or space in a character.

rstrip("parameter as a string")

This is a predefined method used in Python and it accepts character as a parameter to remove the character of the string from the right side.

endswith()

This is a built-in method in Python that returns true if the string ends with a specific value.

Example 1

In this program, we will store the input string in the variable ‘str’. Then initialize the variable ‘i’ to the value 5 which will later trim the character after the 5th index. Next, the variable ‘str’ iterate through the variable ‘char’ using for loop. Then use the if statement to search the whitespace by using isspace() method. If the space is not found in the string it will break the loop and the variable ‘i’ decrement for each whitespace character. Now we are trimming the character by using str[:i] and storing the value in the variable ‘trim_str’. Finally, we are printing the result with the help of the variable ‘trim_str’.

#trim the string from the right
str = "UNIVERSITY"
i = 5
for char in str:
   if not char.isspace():
      break
   i -= 1
trim_str = str[:i] #The use before slicing removes the right string.
print("Trim the string of", i," characters from right:", trim_str)

Output

Trim the string of 5 characters from right: UNIVE 

Example 2

In this program, we will store the input string in the variable ‘my_str’. Then we trim the character ‘a’ from the right side of the string and store it in the variable ‘trim_str’. Finally, we are printing the result with the help of the variable ‘trim_str’.

#Trim the string from right
my_str = "aaaaa!King!aaaaa"
trim_str = my_str.rstrip("a")
print(trim_str)

Output

aaaaa!King!

Example 3

In this program, we will start storing the input string in the variable str_name. Then store the right delete string in the variable del_suffix. Then apply the if-statement to check the condition of string removal of string on right side using a built-in method endswith(). Next, use the replace() method to delete the given string and store it in the variable str_name. Finally, we are printing the variable with the help of str_name.

str_name = "abcdefghi"
del_suffix = "ghi"
if str_name.endswith(del_suffix):
   str_name = str_name.replace(del_suffix, "")
print("After deleting the suffix from left side:",str_name)

Output

After deleting the suffix from left side: abcdef

Example 4

In the following program, we will store the input string in the variable s. Then use the built-in method removesuffix() that set the string named ‘iop’ delete the string from the right side and get printed the result with the help of print() function.

s = 'qwertyuiop'
print(s.removesuffix('iop'))

Output

qwertyu

Conclusion

We understood the difference between the two examples by trimming the string from the left side. We saw there are several different methods used in the examples which are isspace(), rstrip(), endswith(), and slicing technique. The slicing technique is normally used to trim a string from the right side.

Updated on: 01-Jun-2023

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements