Strip in Python is used to remove the first and last characters of a string. Similarly, lstrip is used to remove the characters on the left, and rstrip is used to remove the characters on the right.
All three functions can be passed in a parameter, specifying the first and last characters to be removed.
Note that a character array is passed in. the compiler removes all corresponding characters at both ends until no matching characters exist, for example:
[Python]
TheString = 'saaaay yes no yaaaass'
Print theString. strip ('put ')
TheString is removed from the ['s ', 'A', 'y'] array until the characters are not in the array. Therefore, the output result is:
Yes no
The principle of lstrip is the same as that of rstrip.
Note: When no parameters are input, spaces at the beginning and end are removed by default.
[Python]
TheString = 'saaaay yes no yaaaass'
Print theString. strip ('put ')
Print theString. strip ('put') # There is a space behind the say
Print theString. lstrip ('put ')
Print theString. rstrip ('say ')
Running result:
Yes no
Es no
Yes no yaaaass
Saaaay yes no
Author: johnny710vip