Python s.strip() 究竟做了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13783934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does s.strip() do exactly?
提问by Eric Jung
I was told it deletes whitespace but
有人告诉我它会删除空格,但是
s = "ss asdas vsadsafas asfasasgas"
print(s.strip())
prints out
打印出来
ss asdas vsadsafas asfasasgas
shouldn't it be ssasdasvsadsafasasfasasgas?
不应该ssasdasvsadsafasasfasasgas吗?
回答by Santiclause
You should perhaps check out the docs for the function. It trims leading and trailing whitespace.
" ss ss ".strip()becomes "ss ss"
您或许应该查看该函数的文档。它修剪前导和尾随空格。
" ss ss ".strip()变成"ss ss"
Relevant link: http://docs.python.org/2/library/stdtypes.html#str.strip
相关链接:http: //docs.python.org/2/library/stdtypes.html#str.strip
Additionally, there is a very powerful tool within the Python interactive interpreter - you can do something like help("".strip)to get a bevy of useful information.
此外,Python 交互式解释器中有一个非常强大的工具 - 您可以执行一些操作help("".strip)来获取大量有用的信息。
回答by alinsoar
Definition from Python's reference:
来自Python 参考的定义:
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
返回删除了前导和尾随字符的字符串副本。chars 参数是一个字符串,指定要删除的字符集。如果省略或 None,则 chars 参数默认为删除空格。chars 参数不是前缀或后缀;相反,它的值的所有组合都被剥离了:
回答by Jeff
As others have said, strip()removes leading and trailing spaces only.
正如其他人所说,strip()仅删除前导和尾随空格。
You are looking for s.replace(" ","")
你正在寻找 s.replace(" ","")
回答by Shiv Prakash
Other than deleting leading and trailing whitespaces it strips the passing parameter also.
除了删除前导和尾随空格之外,它还去除了传递参数。
Like the following: string = 'shiv is awesome'
像下面这样: string = 'shiv is awesome'
print(string.strip('sh'))
打印(字符串。带('sh'))
This will print "iv is awesome"
这将打印“iv is awesome”
But it will become very confusing to strip character like this because it leads to various results like look at the following example.
但是像这样剥离字符会变得非常混乱,因为它会导致各种结果,如下例所示。
s = "paandroid is awesomean papapa"
doing s.strip('p')will do nothing
If you give s = "paapandroid is awesomean papa"
doing s.strip('pa')here it gives 'ndroid is awesomean 'with a space in last. What it means that it is recursively deleting any such pacontaining one or more each character on suffixes and prefixes of the string. See some more results by doing strip on "paapandroid is awesomean papa".
s = "paandroid is awesomean papapa"
做s.strip('p')什么也不做
如果你给 s = "paapandroid is awesomean papa"
在这里做s.strip('pa')它给'ndroid is awesomean'最后一个空格。这意味着它正在递归地删除任何包含字符串后缀和前缀上的一个或多个每个字符的任何此类pa。通过在"paapandroid is awesomean papa"上执行 strip 来查看更多结果。
回答by TheSohan
As we know
据我们所知
s.strip()
function returns a string in which leading and trailing spacesare removed or striped
函数返回一个字符串,其中前导和尾随空格被删除或条带化
but if we use strip() function with a argument like-
但是如果我们使用带参数的 strip() 函数,例如 -
s="your string goes here"
s.strip("string")
now as we have passed the argument as "string" this will be treated as a set of char {s,t,r,i,n,g}now similarly: as s.strip() strips the leading and trailing spaces s.strip("string") will search all the char of the set one by one form left and right side of the string s.
现在,当我们将参数作为“字符串”传递时,这将被视为一组 char {s,t,r,i,n,g}现在类似:因为 s.strip() 去除前导和尾随空格 s。 strip("string") 将从字符串 s 的左右两边一一搜索集合中的所有字符。
If a char in the string sisn't in the set than no further checking is done form that side, searching will be continue from the other side till similar thing is happen form that side. if char in string s is found in set than that char is removed, for example-
如果字符串s中的字符不在集合中,则不会从该侧进行进一步检查,将从另一侧继续搜索,直到从该侧发生类似的事情。如果在 set 中找到字符串 s 中的字符,则删除该字符,例如-
consider the string :s="this is tricky"
考虑字符串:s="这很棘手"
s.strip("ric")now for this our set will be {"r","i","c"}.
s.strip("ric")现在我们的集合将是 {"r","i","c"}。
Now checking from left side of string sfirst char is "t", since "t" isn't in our set, we stop here.
现在从字符串的左侧检查第一个字符是“t”,因为“t”不在我们的集合中,我们到此为止。
Now checking from right side of string sfirst char is "y", since "y" isn't in our set , we stop here **output: "this is tricky"
现在从字符串的右侧检查第一个字符是“y”,因为“y”不在我们的集合中,我们停在这里**输出:“这很棘手”
s.strip("ingy")set={"i","n","g","y"} only for right side case y is found and removed after that g isn't found so we stop here **output: "this is trick"
s.strip("ingy")set={"i","n","g","y"} 仅适用于右侧情况 y 被找到并在没有找到 g 后删除所以我们停在这里**输出:“这是把戏”
s.strip("the")set={"t","h","e"} for right side "y" isn't in our set so we stop , for left side "t" is present in our set we remove this, check for "h"-present-so remve "h", than "i" not present stop here
s.strip("the")set={"t","h","e"} 右侧“y”不在我们的集合中,所以我们停止,因为左侧“t”存在于我们的集合中我们删除它,检查“h”-present-so remve“h”,而不是“i”不存在在这里停止
output: "is is tricky"
输出:“这很棘手”

