Python String strip() Method

Example

Remove spaces at the beginning and end of a string:

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

Run Example

Definition and Usage

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

Syntax

string.strip(characters)

Parameter Value

Parameter 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 Example