strip() method

strip can remove the characters on both sides of the string, the syntax

str.strip([chars]);

 

Using the default properties will strip spaces, \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 >>>

Only remove the characters specified at the beginning and end of the string, the middle part will not be removed:

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

 

The 0 in the middle part of the output result still exists:

 1 this is string 0000 

str

.strip([chars]);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019845&siteId=291194637