Python3 strip() Method
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
- chars -- the character sequence to remove from the beginning and end of the string.
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!!!
"
(
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
"
(
str
.
strip
(
'
12
'
)
)
The character sequence is 12
The output of the above example is:
3abcrunoob3
Python3 Strings