A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-string-strip/ below:

Python String strip() Method - GeeksforGeeks

Python String strip() Method

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)



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4