Python Trim String – rstrip(), lstrip(), strip()

ItsMyCode |

Python provides three methods to trim the whitespaces from the string object. Let’s take a look at each of these functions in detail with examples.

Difference between strip() vs rstrip() vs lstrip()

The following methods are used to trim the whitespaces from the string.

  1. strip(): The strip()function removes any leading and trailing whitespace , including tabs (\t), and returns a new string.
  2. rstrip(): The rstrip()function removes any *trailing whitespace * returns a new string that means it eliminates the whitespace at the right side of the string.
  3. lstrip(): The lstrip() function removes any *leading whitespace * returns a new string that means it eliminates the whitespace at the left side of the string.

strip()

The strip() function removes any leading and trailing whitespace , including tabs (\t), and returns a new string.

Syntax: string.strip([chars])

Parameter :

  • chars (optional) **– a string of characters that need to be **removed from the left and right parts of the string.

*Return Value: * Returns a copy of the string with both leading and trailing characters stripped

*Note: * If char arguments are not provided, all the whitespaces on the leading and trailing side are removed from the string.

*Example of strip() function *

# Leading and trailing whitespaces are removed
text1 = ' Python Programming '
print(text1.strip())

# Remove the whitespace and specified character on
# both leading and trailing end
text3 = ' code its my code '
print(text3.strip(' code'))

# Remove the specified character at 
# both leading and trailing end
text2 = 'code its my code'
print(text2.strip('code'))

Output

Python Programming
its my
 its my

rstrip()

The rstrip()function removes any *trailing whitespace * and returns a new string that means it eliminates the whitespace at the right side of the string.

Syntax: string.rstrip([chars])

*Parameter: *

  • chars (optional) **– a string of characters that need to be **removed on the trailing end. (right part of the string)

*Return Value: * Returns a copy of the string with trailing characters stripped.

*Note: * If char arguments are not provided, all the whitespaces on the trailing side are removed from the string.

*Example of rstrip() function *

# Only trailing whitespaces are removed
text1 = ' Python Programming '
print(text1.rstrip())

# Remove the whitespace and specified character at
# trailing end
text2 = ' code its my code '
print(text2.rstrip(' code'))

# Remove the specified character at 
# trailing end
text3 = 'code its my code'
print(text3.rstrip('code'))

Output

Python Programming
       code its my
code its my

lstrip()

The lstrip() function removes any *leading whitespace * and returns a new string that means it eliminates the whitespace at the left side of the string

*Syntax: * string.lstrip([chars])

Parameter :

  • chars (optional) **– a string of characters that need to be **removed on the leading end. (left part of the string)

*Return Value: * Returns a copy of the string with leading characters stripped.

*Note: * If char arguments are not provided, all the whitespaces on the leading side are removed from the string.

*Example of lstrip() function *

# Only leading whitespaces are removed
text1 = ' Python Programming '
print(text1.lstrip())

# Remove the whitespace and specified character at
# leading end
text2 = ' code its my code '
print(text2.lstrip(' code'))

# Remove the specified character at 
# leading end
text3 = 'code its my code'
print(text3.lstrip('code'))

Output

Python Programming   
its my code        
 its my code

17