001_005 Python 去除字符串两端的空格

代码如下:

#encoding=utf-8
print '中国'

#去除字符串两端的空格
str = '  abc 中国   '
print '-'+str.lstrip()+'-'
print '-'+str.rstrip()+'-'
print '-'+str.strip()+'-'

str = 'xx  abc 中国   中国'
print '-'+str.lstrip('x')+'-'
print '-'+str.rstrip('中国')+'-'
str = 'xx  abc 中国   中国xx'
print '-'+str.strip('x')+'-'

str = u'xx  abc 中国   中国'
print '-'+str.lstrip('x')+'-'
print '-'+str.rstrip('中国')+'-'
str = u'xx  abc 中国   中国xx'
print '-'+str.strip('x')+'-'

结果如下:

中国
-abc 中国   -
-  abc 中国-
-abc 中国-
-  abc 中国   中国-
-xx  abc 中国   -
-  abc 中国   中国-
-  abc 中国   中国-
-xx  abc 中国   -
-  abc 中国   中国-

你可能感兴趣的:(001_005 Python 去除字符串两端的空格)