Python String strip() Method

Python strip() method is used to get a copy of the string from which all leading and trailing characters or white spaces on the left and right sides of the string have been removed. It means it does both lstrip() and rstrip() on a string. By default, it removes white spaces.

Syntax:

string_var.strip(characters)
characters(optional) Parameter is a string containing characters to be removed from the beginning and end of the string. if not mentioned, white spaces are removed
Return Value:

The strip() returns a string with all leading and trailing characters(given as argument) or white spaces removed from the left and right sides of the string.

In The following example, you can see that, in the output of the strip() method, leading and trailing white space characters have been removed from string

			

# Python example of the strip() method. 

string = "   tutorialsnation   " 
# Removes spaces from left.
print(string.strip())


				

Output:

tutorialsnation

The following example removes multiple characters from beginning and end of the string

			

# Python example of the strip() method.

string = "$x++==--a@tutorialsnation$x++==--a@" 
# Removes spaces from the left.
print(string.strip("$x+=-a@"))


				

Output:

tutorialsnation