Python - String strip() Method


Advertisements

The string strip() method is used to trim the string. This method has one optional parameter which is used to specify trim character(s). Default value of this parameter is whitespace.

Syntax


string.strip(character(s))

Parameters

character(s) Optional. Character(s) which need to be trimmed from the string. default value is whitespace.

Example:

In the below example, strip() method to used to trim the string using specified trim characters.

MyString = "   Hello World!   "
print(MyString.strip())

MyString = ",*#Hello, World#@!"
print(MyString.strip(",*#@!"))

Output

Hello World!

Hello, World

String Methods
Advertisements