Python String strip() Method (original) (raw)

Last Updated : 01 May, 2025

strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.

Let’s take an example to remove whitespace from both ends of a string.

Python `

s = " GeeksforGeeks " res = s.strip() print(res)

`

**Explanation: s.strip() removes spaces at the start and end of the string.

Syntax

s.strip(_chars)

**Parameters:

**Return Type: A new string with specified characters removed from both ends.

Examples of strip() Method

Removing Custom Characters

We can also use custom characters from the beginning and end of a string. This is useful when we want to clean up specific unwanted characters such as symbols, punctuation, or any other characters that are not part of the core string content

Python `

s = ' ##*#GeeksforGeeks#**## '

res = s.strip('#* ') print(res)

`

**Explanation:

Removing Newline Characters

We can also remove the leading and trailing newline characters ****(\n)** from a string.

Python `

s = '\nGeeks for Geeks\n'

res = s.strip()

print(res)

`

**Explanation: Removes newline characters **\n from both ends of the string.

Strip Tabs and Spaces

Python `

s = '\t Hello World \t' res = s.strip() print(res)

`

**Explanation: Removes both tabs ****(\t)** and spaces from the start and end of the string.

**Related articles: newline characters (\n)