【发布时间】:2016-08-27 17:05:00
【问题描述】:
我正在尝试理解strip() 函数。而且我看到了一个令人困惑的行为。
import sys
test = input().strip('e')
print(test)
N = input().strip('cmowz.')
print(N)
print('www.example.com'.strip('cmowz.'))
这给出了以下输出:
test message here
'www.example.com'
example
所以我看到的是调用input().strip() 方法可以正常工作于尾随和前导空格。但它不适用于其他任何东西。
input().strip('e') 并没有真正从字符串中删除 e。
但是,调用字符串文字 "somethinghere".strip('e') 可以正常工作。
有人能解释一下这种不一致的行为吗?
【问题讨论】:
-
我无法重新创建这个;
N = input().strip('cmowz.')为我输出'example'。 -
Works as documented - 返回字符串的副本删除了前导和尾随字符。
标签: python python-3.x strip