API文档:
Return a copy of the string with leading and 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 both ends of the string this method is called on.
Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.
翻译:
返回字符串的副本,忽略前导指定字符和尾部字符。
如果字符被忽略或没有,空白字符将被删除。如果字符存在,字符必须是一个字符串,字符串中的字符将匹配相关字符,并返回删除匹配的字符后的副本。
2.2.3版本改变:字符参数。通过在早期的2.2版本的字符参数不能。
例子:
#! /usr/bin/env python
#coding=utf-8
formateStr = 'old->%s\t%s\tnew->%s\t%s'
testStr1 = ' abcd '
testStr2 = ' abcd'
testStr3 = 'abcd '
testStr4 = 'AAabcdAA'
testStr5 = 'Aabcd'
testStr6 = 'abcdA'
testStr7 = 'abcd\n'
testStr8 = 'yuiosdfs'
def showStr(testStr,replace=None):
print formateStr % (testStr,str(len(testStr)),testStr.strip(replace),str(len(testStr.strip(replace))))
showStr(testStr1)
showStr(testStr2)
showStr(testStr3)
showStr(testStr4,'A')
showStr(testStr5,'A')
showStr(testStr6,'A')
showStr(testStr7)
showStr(testStr8,'uy')
输出:
old-> abcd 6 new->abcd 4
old-> abcd 5 new->abcd 4
old->abcd 5 new->abcd 4
old->AAabcdAA 8 new->abcd 4
old->Aabcd 5 new->abcd 4
old->abcdA 5 new->abcd 4
old->abcd
5 new->abcd 4
old->yuiosdfs 8 new->iosdfs 6
如果字符被忽略或没有,空白字符将被删除。如果字符存在,字符必须是一个字符串,字符串中的字符将匹配相关字符,并返回删除匹配的字符后的副本。
2.2.3版本改变:字符参数。通过在早期的2.2版本的字符参数不能。
例子:
#! /usr/bin/env python
#coding=utf-8
formateStr = 'old->%s\t%s\tnew->%s\t%s'
testStr1 = ' abcd '
testStr2 = ' abcd'
testStr3 = 'abcd '
testStr4 = 'AAabcdAA'
testStr5 = 'Aabcd'
testStr6 = 'abcdA'
testStr7 = 'abcd\n'
testStr8 = 'yuiosdfs'
def showStr(testStr,replace=None):
print formateStr % (testStr,str(len(testStr)),testStr.strip(replace),str(len(testStr.strip(replace))))
showStr(testStr1)
showStr(testStr2)
showStr(testStr3)
showStr(testStr4,'A')
showStr(testStr5,'A')
showStr(testStr6,'A')
showStr(testStr7)
showStr(testStr8,'uy')
输出:
old-> abcd 6 new->abcd 4
old-> abcd 5 new->abcd 4
old->abcd 5 new->abcd 4
old->AAabcdAA 8 new->abcd 4
old->Aabcd 5 new->abcd 4
old->abcdA 5 new->abcd 4
old->abcd
5 new->abcd 4
old->yuiosdfs 8 new->iosdfs 6