了解python在字符串上的lstrip方法 [英] Understanding python's lstrip method on strings

查看:71
本文介绍了了解python在字符串上的lstrip方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我认为是错误的错误,正在寻求确认,或者我不了解此方法的工作原理.

I ran into what I think is a bug, and I'm looking for confirmation or that I am not understanding how this method works.

这是我的基本输出:

(Pdb) x = 'KEY_K'
(Pdb) x.lstrip('K')
'EY_K'
(Pdb) x.lstrip('KE')
'Y_K'
(Pdb) x.lstrip('KEY')
'_K'
(Pdb) x.lstrip('KEY_')
''
(Pdb) import sys
(Pdb) sys.version
'2.7.11 (default, Dec  5 2015, 14:44:47) \n[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)]'

我的理解是,该示例中的最后一个'lstrip'应该已经返回'K',但是没有.有人知道为什么吗?

My understanding is that the final 'lstrip' in that example should have returned 'K', but it did not. Does anyone know why?

推荐答案

它在文档中是正确的:

lstrip(...) S.lstrip([chars])->字符串或Unicode

lstrip(...) S.lstrip([chars]) -> string or unicode

返回除去前导空格的字符串S的副本. 如果指定了chars而不是None,则删除chars中的字符. 如果chars是unicode,则S在剥离之前将转换为unicode

Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping

'K'位于'KEY_'中,这就是您上一个示例返回''的原因.

'K' is in 'KEY_', that's why your last example returns ''.

请注意,如果'K'如果前面带有不在'KEY_'中的字符,则不会被删除:

Note that 'K' would not have been removed if preceded by a character that is not in 'KEY_':

>>> 'KEY_xK'.lstrip('KEY_')
'xK'

这篇关于了解python在字符串上的lstrip方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆