Python Program to trim a string from both sides


Python has inbuild functions like lstrip(), strip(), and rstrip() that can be used to remove the character from both sides. The term trim the string of characters means to remove the character from the given string.

For example −

Given string is ‘iiiiiiiiiiiBOXERiiiiiiiiii’ and the character ‘i’ from both sides. The final result becomes BOXER.

Syntax

The following syntax is used in the examples −

strip()

This is a predefined method used in Python to trim the character from both sides of the string.

[1:] [:-1]
  • [1:] − This represents the after-slicing on the left side to trim the character.

  • [:-1] − This represents the before by depicting -1 which follows the reverse order of the string and trims the characters from the right side of the string.

lstrip()

This method is used for the left side of the string to trim the character.

rstrip()

This method is used for the right side of the string to trim the character.

replace()

This method

Example 1

In this program, we will store the input string in the variable s_trim. Then use the in-built method named strip() with the variable s_trim and store it in a new variable named trim_str. Finally, we are printing the result with the help of the variable trim_str.

s_trim = "   APPLE   "
trim_str = s_trim.strip()
print( "Trim the whitespace from both left and right sides:", trim_str )

Output

Trim the whitespace from both left and right sides: APPLE

Example 2

A while loop is used in the code to remove a certain character ('a' in this case) from both ends of a string. It checks the initial and end characters of the string for 'a' and removes them by slicing the string if they are. The loop is terminated when the initial and last characters are no longer 'a', and the resulting trimmed string is printed. The trimmed string will be returned, without the character ‘a’ at the beginning or end.

bs_trim = "aaaaPetersonaaaa"
# Set the 0th index match to the character ‘a’
while bs_trim[ 0 ] == "a":
# trim the character from the left side
   bs_trim = bs_trim[ 1: ]
while bs_trim[ -1 ] == "a":
# trim the character from the right side
   bs_trim = bs_trim[ :-1 ]
print( "After trimming the character 'a' from both sides:", bs_trim )

Output

After trimming the character 'a' from both sides: Peterson

Example 3

In this program, we will initialize the variable lr_trim to store the string input. Then take two in-built functions as an object of variable lr_trim that will trim both sides of the given string and store it in the variable trim_str. Finally, we are printing the variable with the help of the variable trim_str.

lr_trim = "k5ktgreSawanKumar543"
trim_str = lr_trim.lstrip( 'k5ktgre' ).rstrip( '543' )
print( "The both sides of the grouping character:", trim_str )

Output

The both sides of the grouping character: SawanKumar

Example 4

In the following example, we will start the program by storing the input string variable lr_trim. Then use the built-in method to replace the specific character and it accepts two parameters- mention the replace character and empty string to set the rest character.

lr_trim = "iiiiiiiiiiiBOXERiiiiiiiiii"
trim_str = lr_trim.replace("i", "")
print( "The both sides of the grouping character:", trim_str )

Output

The both sides of the grouping character: BOXER

Conclusion

The above three outputs show the string by trimming the character from both sides. We discussed the in-built function named strip(), rstrip(), lstrip(), and replace(). Also, we saw how slicing is important while trimming the string from both sides. Therefore, this way we learned the concept of string trimming.

Updated on: 01-Jun-2023

573 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements