Python String strip() Method

Example

Remove spaces at the beginning and end of the string:

txt = "     banana     "
x = txt.strip()
print("of all fruits", x, "is my favorite")

Run Instance

Definition and Usage

The strip() method removes any leading (front) and trailing (end) characters (spaces are the default leading characters to be removed).

Syntax

string.strip(characters)

Parameter Values

Parameters Description
characters Optional. A set of characters, the leading/trailing characters to be removed.

More Examples

Example

Remove leading and trailing characters:

txt = ",,,,,rrttgg.....banana....rrr"
x = txt.strip(",.grt")
print(x)

Run Instance