我想知道rstrip和lstrip在python中是如何工作的。 如果我有一根绳子
>> a = 'thisthat'
>> a.rstrip('hat')
out : 'this'
>> a.lstrip('this')
out: 'at'
>> a.rstrip('cat')
out: 'thisth'
如何删除一个单词?在
理想的输出应该是“thist”(第一种情况)和“that”(第二种情况)对吗?在
还是像正则表达式一样,匹配每个字符,以及它在末尾看到的字符(对于第三种情况)?在
注意:我不是在寻找replace,我只想知道strip如何工作,同时从字符串中剥离子字符串。在
等于
^{pr2}$从doc:
strip
接受一个字符参数,并将尽可能长时间地从两端删除这些字符。在参数被视为一组字符,而不是子串。在
lstrip和rstrip的工作方式完全相同,只是其中一个只从左侧剥离,另一个从右侧剥离。在
lstrip
、rstrip
和strip
分别从字符串的左、右和两端删除字符。默认情况下,它们删除空白字符(空格、制表符、换行符等)您可以使用chars参数更改它删除的字符。在
^{pr2}$然而,根据你的问题,听起来你实际上是在寻找替代品
相关问题 更多 >
编程相关推荐