Easy Tutorial
❮ Python Reversing List Python Os Makedev ❯

Python3 strip() Method

Python3 Strings


Description

The Python strip() method is used to remove the specified characters from the beginning and end of a string (default is whitespace).

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

Syntax

The syntax for the strip() method is:

str.strip([chars]);

Parameters

Return Value

Returns a new string with the specified character sequence removed from the beginning and end of the original string.

Example

The following example demonstrates the use of the strip() function:

Example (Python 3.0+)

#!/usr/bin/python3

str = "*****this is **string** example....wow!!!*****"
print (str.strip( '*' ))  # Specifies the character *

The output of the above example is as follows:

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

Notice that the characters in the middle are not removed.

The following example demonstrates the removal of characters if they are found at the beginning or end of the string:

Example (Python 3.0+)

#!/usr/bin/python3

str = "123abctutorialpro321"
print (str.strip( '12' ))  # The character sequence is 12

The output of the above example is as follows:

3abctutorialpro3

Python3 Strings

❮ Python Reversing List Python Os Makedev ❯