Strip in python is used to remove the first character of a string. Similarly, lstrip is used to remove the character on the left, and rstrip is used to remove the character on the right. All three functions can be passed in a parameter, specifying the first and last characters to be removed. Note that an array of characters is passed in. the compiler removes all corresponding characters at both ends until no matching characters exist, for example:
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.
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