python之strip、lstrip、rstrip

下边将给大家展示python如何使用strip、lstrip、rstrip函数
这三个函数是从字符串两端、左侧、右侧删除不想要的字符,但是默认不设置时候是删除空格、制表符和换行符。

比如:

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

a的左侧含有空格,右侧含有制表符 \t 几个空格和换行符 \n (如果你以前没有见过\t和\n,那么现在知道了这是计算机中表示制表符和换行符的方法) 注意:单独输出a,你看不到句子后边的制表符和空格,但是他们确实存在。

附上代码帮助理解

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 .'

猜你喜欢

转载自blog.csdn.net/m0_50481455/article/details/109155756