In Python, there are three functions that remove the kinsoku, whitespace characters, which are:
Strip: Used to remove Kinsoku, whitespace characters (including \ n, \ r, \ t, "', i.e.: newline, carriage return, tab, space)
Lstrip: Used to remove opening characters, white space characters (including \ n, \ r, \ t, "', i.e.: newline, carriage return, tab, space)
Rstrip: Used to remove trailing characters, whitespace (including \ n, \ r, \ t, ' ', i.e.: newline, carriage return, tab, space)
Note: These functions will only delete the header and tail characters, and the middle one will not be deleted.
The usage is:
String.strip ([chars])
String.lstrip ([chars])
String.rstrip ([chars])
The parameter chars is optional, and when chars is empty, the whitespace characters (including \ n, \ r, \ T, ") are removed by default for the string tail
When chars is not empty, the function is chars into characters and then the characters are removed.
It returns a string copy that removes the kinsoku (or whitespace) characters, and the string itself does not change.
Examples are as follows:
1. When chars is empty, the default is to remove the white space character (including ' \ n ', ' \ R ', ' \ t ', ')
>>> str = " AB CD " >>> str AB CD " >>> Str.strip () Span style= "COLOR: #008000" ># Remove the Kinsoku space ab cd " >>> Str.lstrip () Span style= "COLOR: #008000" ># remove opening spaces ab cd " >>> Str.rstrip () Span style= "COLOR: #008000" ># delete trailing spaces AB CD "
2. When chars is not empty, the function is chars into characters and then the characters are removed.
>>> str2 ='1a2b12c21'>>> Str2.strip (' A')#Remove 1 and 2 from the tail'a2b12c'>>> Str2.lstrip (' A')#Delete 1 and 2 at the beginning'A2b12c21'>>> Str2.rstrip (' A')#Delete End of 1 and 2'1a2b12c'
Strip (), Lstrip (), Rstrip () usage in Python