In python, the string function strip() gives a copy of the string with leading and trailing spaces removed. In this tutorial we’ll be looking its syntax and use-cases along with some examples.
Before we proceed, here’s a quick refresher on python strings – Strings are immutable sequences of unicode characters used to handle textual data. Strings implement all common sequence operations (example, iterations, membership tests, etc). For more, check out our guide on strings and other fundamentals of python.
The strip() function
As stated already,
strip() is a python string function used to remove leading and trailing spaces but it can be used to remove any given characters from the starting and end of the string. It is quite frequently used for text formatting purposes, for example, cleaning texts in forms filled out by users. The following is the syntax:
Here, sample_string is the string you want to truncate the leading and trailing chars from.
- chars (optional): String specifying the set of characters to be removed. If it’s not provided, it defaults to removing all the leading and trailing whitespace characters.
Returns: It returns a copy of the string with the leading and trailing chars removed.
Note: Strings are immutable and hence cannot be changed, string functions like
strip() return a copy of the string with its operations performed.
Example 1: Using the strip() function without any arguments
# strip() function to remove whitespace characters s1 = "Python strings are immutable" s2 = " Python strings are immutable " s3 = " \t\tPython strings are immutable \t\t" # using the strip() function on the above strings print(s1.strip()) print(s2.strip()) print(s3.strip())
Python strings are immutable Python strings are immutable Python strings are immutable
In the above example, the return values from applying the
strip() function on strings
s3 are the same because, by default, the
strip() function removes all the leading and trailing whitespace characters. Note that the tab space character
'\t' is also one of the whitespace characters in python.
Example 2: Using the strip() function to remove custom characters
# strip() function to remove custom characters s = "c co cod code gives bugs on running the code cod co c" # using the strip() function print(s.strip(' code'))
gives bugs on running th
In the above example, we pass the string
' code' as an argument to the
strip() function. This results in the function removing all instances of characters
'e' from the starting and end of the string
For more on string functions refer to the python docs.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.