Python String strip() Method

Python string strip() method “returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).”

Syntax

string.strip([character])

Parameters

character: It is an optional parameter which is the String specifying the characters to be removed. If the character argument is not provided, all leading and trailing whitespaces are removed from the String.

Return value

The string strip() method returns the String copy with both leading and trailing characters stripped.

Example 1: How to Use string strip() Method

data = ' Winter is coming!! '

print(data.strip())

Above code, we have put the space at the start of the String and two spaces at the end.

Output

Python String Strip Example

Example 2: Giving no arguments

If we give no arguments to the strip() method, the leading and trailing whitespaces will be stripped.

str = " database is good! ";

print(str.strip())

Output

database is good!

Example 3: Case sensitive

The string.strip() method is case-sensitive. When we pass a character argument to the method, it will not strip the same character with a different case.

str = "hey, this is bart simpson, the coolest Simpson";

print(str.strip('simpson'))

Output

hey, this is bart simpson, the coolest S

That’s it.

1 thought on “Python String strip() Method”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.