python之strip、lstrip、rstrip

The following will show you how python uses the strip, lstrip, and rstrip functions.
These three functions are to delete unwanted characters from the two ends, left and right sides of the string, but the default is to delete spaces, tabs, and newlines when they are not set. symbol.

such as:

a = " Remove unwanted characters   from tring .\t\t    \n"

There is a space on the left side of a, and a tab character on the right side. \t Several spaces and newline characters\n (If you haven’t seen \t and \n before, then you now know that this is a computer that represents tabs and newlines. Note: If you output a alone, you can’t see the tabs and spaces after the sentence, but they do exist.

Attach the code to help understand

a1 = a.lstrip()
#则a1为
#'Remove unwanted characters   from tring .\t\t    \n'

#如果感受不清楚,令a左侧无空格
a = "Remove unwanted characters   from tring .\t\t    \n"
a2 = a.lstrip("R")
#则a2为
#'emove unwanted characters   from tring .\t\t    \n'
#R被删除掉了

a = " Remove unwanted characters   from tring .\t\t    \n"
a3 = a.strip()
#则a3为
#'Remove unwanted characters   from tring .'

Guess you like

Origin blog.csdn.net/m0_50481455/article/details/109155756