python当中strip()是起什么作用的?举例说明一下,谢谢

2025-02-26 00:22:35
推荐回答(5个)
回答1:

告知你一个捕鱼的方法:
>>> import string
>>> help(string.strip)
Help on function strip in module string:
strip(s, chars=None)
strip(s [,chars]) -> string

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.
用help()查询 模块,方法等的用途。

回答2:

string.rstrip(s[,
chars])
Return a copy of the string with trailing characters removed. If
chars is omitted or None, whitespace characters are removed. If given and not
None,
chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.
例子:
s='abcdefg'
s=s.strip("asbdg")
print s
结果为'cdef'
默认的是去除空白字符包括' ','\t','\n'等

回答3:

去掉字符串前后空格

回答4:

去掉前后的空格

回答5:

s=" fsdkjfdkfk kdf ls kfjsdlk "
s=s.strip()
结果s就是"fsdkjfdkfk kdf ls kfjsdlk"前后空格都没了,但中间间隔还保留。