Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

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

|| 0

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

How to trim string in Python?

String can be trimmed in python using the following Python function:

  • strip()
  • rstrip()
  • lstrip()

strip() function in Python

The strip() method in Python is used for removing the trailing and leading characters within 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 removed. The strip() function can take no parameters too. In that case, the leading and trailing spaces are deleted from the string.

But in case the combination of the strings inside remove do not match with the combination of the actual string, then no modification is performed by the function. If the remove arguments match, then the function returns the string without the spaces and characters.

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

rstrip() function in Python

The rstrip() function is similar to the strip() function is used to return a string with the specified characters and spaces removed from the right. The syntax used in this case is:

string.rstrip([remove])

In this function, the remove argument specifies the characters that need to be removed. If this argument is not 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.

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

lstrip() function in Python

When a lstrip() function is used on a string, all the white spaces and characters starting from the left will be removed. The function will return a copy of the string that will have the characters specified in the parameter deleted. The syntax for the function is:

string.lstrip([remove])

where the remove argument specifies the characters to be stripped from the string. The characters are removed until the initial mismatch. But if no arguments are provided, all the whitespaces are removed.

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

 


Advertisement
Advertisement
Advertisement