Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 strip() Method

Python3 strip() Method

Python3 Strings Python3 Strings


Description

The Python strip() method removes specified characters (defaults to spaces) or a character sequence from the beginning and end of a string.

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

Syntax

Syntax of the strip() method:


str.strip([chars]);

Parameters

Return Value

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

Example

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

Example (Python 3.0+)

#!/usr/bin/python3

str

=

"

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

"

print

(

str

.

strip

(

'

'

)

)

Specify the string *

The output of the above example is:


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

From the result, you can notice that characters in the middle were not removed.

The above example demonstrates that characters are removed as long as the beginning and end contain characters from the specified sequence:

Example (Python 3.0+)

#!/usr/bin/python3

str

=

"

123abcrunoob321

"

print

(

str

.

strip

(

'

12

'

)

)

The character sequence is 12

The output of the above example is:


3abcrunoob3

Python3 Strings Python3 Strings