lstrip python_python – str.lstrip()意外行为

我正在研究scrapy项目,并试图解析我的配置

字符串是attr_title我必须去掉’attr_’并获得标题.我使用了lstrip(‘attr_’),但得到了意想不到的结果.我知道lstrip可以解决这些问题并删除它们,但很难理解它.

In [17]: "attr.title".lstrip('attr.')

Out[17]: 'itle'

PS:我知道有多种提取字符串的解决方案我有兴趣了解这一点.

解决方法:

lstrip遍历结果字符串,直到没有更多的组合与最左边的字符集匹配

下面有一个小插图.

In [1]: "attr.title".lstrip('attr.')

Out[1]: 'itle' # Flow --> "attr." --> "t" --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itle') is returned

In [2]: "attr.tritle".lstrip('attr.')

Out[2]: 'itle' # "attr." --> "t" --> "r" --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itle') is returned

In [5]: "attr.itratitle".lstrip('attr.')

Out[5]: 'itratitle' # "attr." --> Next char is 'i' which does not match any combination hence, iteration stops & end result ('itratitle') is returned

标签:python

来源: https://codeday.me/bug/20190713/1453684.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值