Skip to main content

strip()

The strip() method in Python is used to remove any leading (spaces at the beginning) and trailing (spaces at the end) characters from a string. By default, it removes any whitespace characters like spaces, tabs, or newlines, but you can also specify the characters to be removed as an argument within the method.

Parameter Values

Parameter Description
chars (optional)

A string specifying the set of characters to be removed from the beginning and end of the original string. If not provided, all leading and trailing whitespaces are removed.

Return Values

The strip() method returns a new string with leading and trailing whitespace removed.

How to Use strip() in Python

Example 1:

The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default) from a string.

text = '   Hello, World!   '
print(text.strip())
Example 2:

You can specify which characters to remove by passing them as an argument to strip() method.

text = '***Hello, World!***'
print(text.strip('*'))
Example 3:

The strip() method does not modify the original string, it returns a new string with the leading and trailing characters removed.

text = 'Hello, World!'
new_text = text.strip()
print(new_text)