Python string | band ()

strip () — is a built-in function in the Python programming language that returns a copy of a string with both leading and trailing characters removed (based on the passed string argument). 
Syntax :

 string.strip ([chars])  Parameter:  There is only one optional parameter in it: 1)  chars  - a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string.  Return Value:  Returns a copy of the string with both leading and trailing characters removed. 

Exit :

 geeks for geeks geeks for geeks for 

Errors :
When we try to delete everything but the line, a runtime error occurs. The system produces output showing

# Python3 program to demonstrate usage
# strip () method

 

string = "geeks for geeks"  

 
# prints a line without stripping

print (string) 

 
# prints the string, removing leading and trailing spaces

print (string.strip ()) 

 
# prints a line, removing geeks

print (string.strip ( `geeks` ))

# Python3 program to demonstrate usage
Method error # strip ( )

 

string = " geeks for geeks "

list = [ 1 , 2 , 3

 
# prints an error message

print ( list . strip ()) 

Exit :

 Traceback (most recent call last): File "/ home / 1bdead5a6ad1dbb0db1c6a2663874e4c. py ", line 8, in print (list.strip ()) AttributeError: type object` list` has no attribute `strip` 

Practical use:
By given line, remove the occurrence of the word “The” from the beginning and the end.

# Python3 practical demonstration program
# strip ()

 

 

string = "the King has the largest army in the entire world the " 

  
# prints the line after deletion from the beginning and end

print (string.strip ( "the" ))

Output:

 King has the largest army in the entire world