Home > python > Python trim python strip function

Python Trim

Sometime we may come across a requirement to remove spaces from the string. Different programming languages provides functions for this purpose. Removing white spaces from the start or end of the string is called trimming and most programming languages provide a function named trim () for this purpose. Since we have strings in python so we also require a function to for trimming the strings. Python doesn’t have a trim function for this purpose. In contrast to python trim function we have three other functions available for trimming the strings. strip (), lstrip (), rstrip ().  These functions not only remove the white spaces from the strings but also removes any characters provided to them as arguments. Below is the syntax for these functions.

Python  strip () function

string.strip([chars]) 

This will remove the given characters from both the right or left side of the array. It takes a character array as an optional argument. So if we want to remove white spaces from the string we will leave the arguments empty.

python trim python strip

Python lstrip () function

string.lstrip ([chars])

lstrip is used to remove the characters from the left side of the string. Like strip () argument for lstrip is also optional. And to remove white spaces we can leave the argument.

python lstrip

Python rstrip () function

string.rstrip ([chars])

we can use rstrip to remove the characters from the right side of the string. Like strip () and lstrip () argument for rstrip is also optional. And to remove white spaces we can leave the argument.

python rstrip

Note: All three functions return the copy of the string with the provided characters stripped from the string.

Python Trim / Python Strip Examples

str = ' this is a string   '
print("Before removing the white spaces")
print (str)
# Using strip to remove the white spaces
print("After removing the white spaces")
print(str.strip())

Output:

Before removing the white spaces

 this is a string  

After removing the white spaces

this is a string

str = ' this is a string   '
print("Before removing the white spaces from left")
print (str)
# Using lstrip to remove the white spaces
print("After removing the white spaces from left")
print(str.lstrip())

Output:

Before removing the white spaces from left

 this is a string  

After removing the white spaces from left 

this is a string  

str = ' this is a string   '
print("Before removing the white spaces from right")
print (str)
# Using rstrip to remove the white spaces
print("After removing the white spaces from right")
print(str.rstrip())

Output:

Before removing the white spaces from right

 this is a string  

After removing the white spaces from right

this is a string

Removing strings from the string

str = 'this is a string remove this from both side'
print("Before removing the string")
print (str)
# Using strip to remove the string
print("After removing the string") 
print(str.strip("this"))

Output:

Before removing the string

this is a string remove this from both side

After removing the string

is a string remove this from both side

since no this is present at the right side so it only removed it from the left side.

str = 'this is a string remove this from left side'
print("Before removing the string")
print (str)
# Using lstrip to remove the string from left
print("After removing the string")
print(str.lstrip("this"))

Output:

Before removing the string

this is a string remove this from left side

After removing the string

is a string remove this from left side

str = 'this is a string remove this from right side'
print("Before removing the string")
print (str)
# Using rstrip to remove the string from right
print("After removing the string")
print(str.rstrip("this"))

Output:

Before removing the string

this is a string remove this from right side

After removing the string

this is a string remove this from right side

As no matching string was found on the right side so it returned the original string.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: