Online Tutorials & Training Materials | STechies.com
Register Login

Python String Trim using strip(), rstrip() and lstrip() with example

29 Jul 2019 2:08 pm || 0

Python Trim String Methods

A string can be trimmed in python using the following three Python function:

  • strip(): removes the trailing and leading characters, whitespaces including tabs(t) from the string
  • rstrip(): removes the trailing characters and whitespaces from the right side of the string
  • lstrip(): removes the leading characters and whitespaces from the left side of the string

Note: All the above Python functions take no argument to remove whitespaces from the string, whereas to remove a character from the string, an [remove] argument should pass, which specifies the character to be stripped from the string.

Python strip() function

The Python strip() method returns a new string after removing whitespaces, trailing and leading characters from a string. The syntax used in this case is:

string.strip([remove])

Here, remove is an optional argument that specifies the set of characters that have to be trimmed.

If in case no parameter is defined, then the leading and trailing whitespaces will be deleted from the string.

Also, in case if the combination of the strings inside [remove] argument does not match with the combination of the actual string, then no modification is performed by the function.

STRIP=""

print("****** WHITE SPACE REMOVE WITH strip FUNCTION ******")

str = "     STechies - Free Training Tutorials for Techie      "

print("STRING WITHOUT strip : " + str)

print("STRING WITH strip : " + str.strip())

OUTPUT :

 ****** WHITE SPACE REMOVE WITH strip FUNCTION ******
    STRING WITHOUT strip :           STechies - Free Training Tutorials for Techie           
    STRING WITH strip : STechies - Free Training Tutorials for Techie

Python rstrip() function

The rstrip() function is similar to the strip() function that returns a new string with the specified characters and whitespaces removed from the right side of the string. The syntax used in this case is:

string.rstrip([remove])

In this function, the [remove] argument specifies the characters that is needed to be trimmed.

If no argument is provided, then the white spaces from the right side of the string are deleted. All the combinations of the remove argument are deleted from the actual string until a mismatch occurs.

RSTRIP=""

print("****** WHITE SPACE REMOVE WITH rstrip FUNCTION ******")

str = "STechies - Free Training Tutorials for Techie      "

print("STRING WITHOUT rstrip : " + str)

print("STRING WITH rstrip : " + str.rstrip())

OUTPUT:

  ****** WHITE SPACE REMOVE WITH rstrip FUNCTION ******
    STRING WITHOUT rstrip : STechies - Free Training Tutorials for Techie           
    STRING WITH rstrip : STechies - Free Training Tutorials for Techie

Python lstrip() function

When a lstrip() function returns a new string after removing all the whitespaces and characters starting from the left side of the string.

The function will return a copy of the string that will have the characters specified in the parameter deleted. The syntax for this function is:

string.lstrip([remove])

The [remove] argument specifies the characters to be stripped from the string. The characters are trimmed from the left of the string until the initial mismatch occurs.

if no arguments are specified, all the whitespaces will be removed from the left side of the string.

LSTRIP=""
 
print("****** WHITE SPACE REMOVE WITH lstrip FUNCTION ******")

str = "    STechies - Free Training Tutorials for Techie"

print("STRING WITHOUT lstrip : " + str)

print("STRING WITH lstrip : " + str.lstrip())

OUTPUT :

  ****** WHITE SPACE REMOVE WITH lstrip FUNCTION ******
    STRING WITHOUT lstrip :         STechies - Free Training Tutorials for Techie
    STRING WITH lstrip : STechies - Free Training Tutorials for Techie