Python String strip() Method

❮ String Methods


Example

Remove spaces at the beginning and at the end of the string:

txt = "     banana     "

x = txt.strip()

print("of all fruits", x, "is my favorite")
Try it Yourself »

Definition and Usage

The strip() method removes any leading, and trailing whitespaces.

Leading means at the beginning of the string, trailing means at the end.

You can specify which character(s) to remove, if not, any whitespaces will be removed.


Syntax

string.strip(characters)

Parameter Values

Parameter Description
characters Optional. A set of characters to remove as leading/trailing characters

More Examples

Example

Remove the leading and trailing characters:

txt = ",,,,,rrttgg.....banana....rrr"

x = txt.strip(",.grt")

print(x)
Try it Yourself »

❮ String Methods

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.