今天在写代码的时候碰到stripe()方法,对此不是很了解,写着记录一下
strip()
文档里面是这么描述的:
Return a copy of the string with leading and trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
返回删除了前导和尾随空格的字符串副本。
如果给定了字符而不是None,改为删除指定字符。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
str = "00000003210Runoob01230000000";
print str.strip( '0' ); # 去除首尾字符 0
str2 = " Runoob "; # 去除首尾空格
print str2.strip
结果如下:
3210Runoob0123
Runoob
同时它也有衍生的lstrp()和rstrip()方法、
# chars --指定截取的字符。
str.lstrip([chars])
返回截掉字符串左边的空格或指定字符后生成的新字符串。rstrip()与之类似