[英]Why I am getting these different outputs?
print('xyxxyyzxxy'.lstrip('xyy'))
# output:zxxy
print("xyxefgooeeee".lstrip("efg"))
# ouput:xyxefgooeeee
print('reeeefooeeee'.lstrip('eeee'))
# output:reeeefooeeee
Here for the last two print statements, I am expecting output as a first print statement, as it has stripped 'xyxxyy', but in the last two print statements, it is not stripping in the same way as it has done in first.对于最后两个打印语句,我期望 output 作为第一个打印语句,因为它已经剥离了“xyxxyy”,但在最后两个打印语句中,它并没有像第一次那样剥离。 Please tell me why it so?
请告诉我为什么会这样?
In Python leading characters in Strings containing xyy are removed because of.lstrip().在 Python 中,包含 xyy 的字符串中的前导字符因 .lstrip() 而被删除。 For example:
例如:
txt = ",,,,,ssaaww.....banana"
x = txt.lstrip(",.asw")
print(x)
The output will be: banana output 将是:香蕉
string.lstrip(chars)
removes characters from the left size of the string until it reached a character that does not appear in chars
. string.lstrip(chars)
从字符串的左侧大小中删除字符,直到它到达一个未出现在chars
中的字符。
In your second and third examples, the first character of the string does not appear in chars
, so no characters are removed from the string.在您的第二个和第三个示例中,字符串的第一个字符没有出现在
chars
中,因此不会从字符串中删除任何字符。
I think because the order of char doesn't matter.我认为因为 char 的顺序无关紧要。
xyy
or yxx
will result in the same thing. xyy
或yxx
将导致相同的结果。 It will remove chars from the left side until it sees a char that is not included.它将从左侧删除字符,直到看到未包含的字符。 For example:
例如:
print('xyxxyyzxxy'.lstrip('xyy'))
zxxy
print('xyxxyyzxxy'.lstrip('yxx'))
zxxy
In fact, if you only used 2 chars 'xy' or 'yx' you will get the same thing:事实上,如果你只使用 2 个字符 'xy' 或 'yx' 你会得到同样的结果:
print('xyxxyyzxxy'.lstrip('xy'))
zxxy
In the other cases, the first left char is not included, therefore there's no stripping在其他情况下,不包括第一个左字符,因此没有剥离
I just got to know lstrip() removes, all combinations of the characters passed as an argument are removed from the left-hand side.我刚刚知道 lstrip() 删除,作为参数传递的所有字符组合都从左侧删除。
lstring using the set of the chars in the string and then removes the all characters from the primary string start from the left lstring 使用字符串中的字符集,然后从左侧开始的主字符串中删除所有字符
print('xyxefgooeeee'.lstrip('yxefg'))
"""In 'xyxefgooeeee' the first char is 'x' and it exists in the 'yxefg' so
will be removed and then it will move to the next char 'y','x','x','e','f',
'g' and then 'o' which doesn't exist. therefore will return string after 'o'
"""
OutPut : ooeeee
print('xyxefgooeeee'.lstrip('efg'))
"""In the xyxefgooeeee' the first char 'x' does to exist in the 'efg' so will
not be removed and will not move to the next char and will return the
entire primary string
"""
OutPut: xyxefgooeeee
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.