Python中lstrip使用心得

       lstrip方法用来去除字符串从首位开始与之相匹配的字符。例如:

a = 'c'
b = 'calendar'
print(b.lstrip(a))

       输出结果是 'alendar'。


之前我也一直是这样用的,把它看成是去除匹配的“字符串”。那么问题来了,今天在处理字符串时,用lstrip总是会多砍掉字符,代码如下:

a = 'c:/svncode'
b = 'c:/svncode/calendar'
print(b.lstrip(a))


       我预期的输出结果是'/calendar',可是实际的输出结果是'alendar'……于是我陷入了沉思…终于找到了原因!lstrip方法只是比对字符并去除,而不是字符串!因此当b使用lstrip剩下'/calendar'的时候,会继续进行,a中有'/',因此'/calendar'变成'calendar',a中有'c',因此'calendar'变成'alendar'。

       如果真是这样,那么如果 a = ':/svnodec' ,b.lstrip(a)的结果也是'alendar',我试了一下,果真如此!!





posted on 2014-10-17 11:38  Noah.Zhang  阅读(4313)  评论(0编辑  收藏  举报

导航