Python 条带方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14646436/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:02:28  来源:igfitidea点击:

Python strip method

python

提问by Rajesh Kumar

Today in python terminal, I tried

今天在python终端,我试过了

a = "serviceCheck_postmaster"
a.strip("serviceCheck_")

But instead of getting "postmaster", I got "postmast".

"postmaster"我没有得到,而是得到了"postmast"

What could cause this? And how can I get "postmaster"as output?

什么可能导致这种情况?我怎样才能得到"postmaster"输出?

回答by Martijn Pieters

You are misunderstanding what .strip()does. It removes anyof the characters found in the string you pass. From the str.strip()documentation:

你是误会了什么.strip()。它删除在您传递的字符串中找到的任何字符。从str.strip()文档

The charsargument is a string specifying the setof characters to be removed.

字符参数是字符串指定一组待删除的字符。

emphasis mine; the word setthere is crucial.

强调我的;这个词set很关键。

Because charsis treated as a set, .strip()will remove all s, e, r, v, i, c, C, h, kand _characters from the start and end of your input string. So the eand rcharacters from the endof your input string were also removed; those characters are part of the set.

因为chars作为一组处理,.strip()将删除所有servicChk_从你的输入字符串的开始和结束字符。因此,输入字符串末尾er字符也被删除了;这些字符是集合的一部分。

To remove a string from the start or end, use slicing instead:

要从开头或结尾删除字符串,请改用切片:

if a.startswith('serviceCheck_'):
    a = a[len('serviceCheck_'):]

回答by Harpal

An alternative to Martijn's answer to would to use str.replace()

Martijn's answer to would to use 的另一种选择 str.replace()

>>> a = "serviceCheck_postmaster"
>>> a.replace('serviceCheck_','')
'postmaster'

回答by Todd

You can also isolate "postmaster" with something like this:

您还可以使用以下内容隔离“postmaster”:

a = "serviceCheck_postmaster"
b = a.split("_")[1]             # split on underscore and take second item

回答by kvivek

If you carefully look at the help of the strip function It says like this:

如果仔细看strip函数的帮助,是这样写的:

Help on built-in function strip:

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

    Return a copy of the string S with leading and trailing
    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

It will remove all the leading and trailing character and whitespaces. In your case the character sets are

它将删除所有前导和尾随字符和空格。在你的情况下,字符集是

s, e, r, v, i, c, C, h, k and _

You can get the postmaster by something like this

你可以通过这样的方式得到邮局局长

a = "serviceCheck_postmaster"
print a.split("_")[1]

回答by Akinsete akin

Strip will take away all the letter you input into its function. So for the reason why the letter ‘e' and ‘r' were stripped of postmaster. Try: a = “serviceCheck_postmaster” print(a[13:])

Strip 将带走您在其功能中输入的所有字母。所以因为字母'e'和'r'被剥夺了postmaster的原因。尝试: a = “serviceCheck_postmaster” print(a[13:])