VOOZH about

URL: https://www.geeksforgeeks.org/python/python-string-strip/

⇱ Python String strip() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python String strip() Method

Last Updated : 1 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.


Output
GeeksforGeeks

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

Syntax

s.strip(chars)

Parameters:

  • chars(optional): A string specifying the set of characters to remove from the beginning and end of the string.

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


Output
GeeksforGeeks

Explanation:

  • strip('#* ') removes any #, *, and spaces from both beginning and end of the string.
  • It stops stripping characters from both end once it encounters a character that are not in the specified set of characters.

Removing Newline Characters

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


Output
Geeks for Geeks

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

Strip Tabs and Spaces


Output
Hello World

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

Related articles:newline characters (\n)

Comment
Article Tags: