strip()方法

strip可以去除字符串两边的字符,语法

str.strip([chars]);

使用默认属性会去掉空格,\t, \n

 1 >>> a = '\nabc\n'
 2 >>> a.strip()
 3 'abc'
 4 >>> a = ' abc '
 5 >>> a
 6 ' abc '
 7 >>> a.strip()
 8 'abc'
 9 >>> a = '\tabc\t'
10 >>> a.strip()
11 'abc'
12 >>> a = '\aabc\a'
13 >>> a.strip()
14 '\x07abc\x07'
15 >>> a
16 '\x07abc\x07'
17 >>>

只移除字符串头尾指定的字符,中间部分不会移除:

1 #!/usr/bin/python
2 
3 str = "0000000this is string 0000example....wow!!!0000000";
4 print str.strip( '0' );

输出结果中间部分的 0 还是存在的:

 1 this is string 0000 

str

.strip([chars]);

猜你喜欢

转载自www.cnblogs.com/ducklu/p/8966037.html