Python3 strip() Method

Python3 字符串Python3 Strings


Description

Python strip() method is used to remove specified characters (default is space) or character sequences from the beginning and end of a string.

Note:This method can only delete characters at the beginning or end, not characters in the middle part.

Syntax

Syntax of the strip() method:

str.strip([chars]);

Parameters

  • chars -- The specified sequence of characters to remove from the beginning and end of the string.

Return Value

Returns a new string generated by removing the specified character sequence from the beginning and end of the string.

Example

The following example demonstrates how to use the strip() function:

Example (Python 3.0+)

#!/usr/bin/python3 str = "*****this is **string** example....wow!!!*****" print (str.strip( '*' )) # Specify string *

The output of the above example is as follows:

this is **string** example....wow!!!

From the results, it can be noted that the characters in the middle part were not deleted.

The following example demonstrates that characters are deleted as long as the beginning and end contain characters from the specified character sequence:

Example (Python 3.0+)

#!/usr/bin/python3 str = "123abcexample321" print (str.strip( '12' )) # Character sequence is 12

The output of the above example is as follows:

3abcexample3

Python3 字符串Python3 Strings

Other Extensions