Suppose we add a few extra spaces or unwanted text or characters while storing the data in the variable and we want to remove them without going back to the program and changing their values manually. To resolve this issue, Python provides a pre-defined function called as strip() function. This Python strip() function is used to eliminate the unwanted spaces and texts from the starting and the ending position of the data value. Let us look at the syntax, its uses, and a few examples to understand this concept in detail.
- Python strip() function returns a string by eliminating both the leading and trailing characters or empty whitespaces as per the arguments passed to the function.
- Python’s built-in strip() function is used to removes the given characters as well as whitespaces from the start and end of the original string based on the arguments passed.
Python strip() Function
Python strip() function is used to remove extra whitespaces and specified characters from the start and from the end of the strip irrespective of how the parameter is passed. The strip() function also accepts an argument that specifies the list of characters to be removed from the original string. To use this function, let us look at its syntax first.
string = the variable name in which the string is stored
chars = a string specifying the set of characters to be removed from the original string
Note 1 – The ‘chars’ parameter is optional. If we do not specify the ‘chars’ parameter, then the strip() function will only remove all the leading and trailing whitespaces from the string.
Note 2 – The strip() function in Python is only used on string iterators. It cannot be used on any other data types such as lists, tuples, sets, dictionary, etc.
Return Value from the strip() Method
The strip() fiction returns the following values –
- It returns the original string as it is with the whitespaces removed from both the left and right sides of the string if the characters are not specified.
- If the string does not contain any whitespaces at the start or the end, the strip() function will return the exact same string as the output.
- In the argument, if the characters to be removed are defined, and the same characters given, match the string value, then, those characters at the start and end of the string will be eliminated and the rest of the string will be returned.
- If the characters specified do not match with the values in the variable string, then the original string will be returned.
Let us understand each one of these return values by looking at the below examples.
Python strip() Examples
# strip() without character parameter str = ' Hello! Welcome Home. ' print(str) print(str.strip())
Hello! Welcome Home. Hello! Welcome Home.
Note – The strip() function does not change the original value of the variable. It returns a new string after removing the whitespaces on either side of the string.
txt = ' Ferrari ' x = txt.strip() print('I am buying a', txt, 'tomorrow') print('I am buying a', x, 'tomorrow')
I am buying a Ferrari tomorrow I am buying a Ferrari tomorrow
The strip() function only works for variables whose inputs are strings only. The function will not work for other data types. Look at the below example for more clarity.
# strip() function for other data types data = [ 1, 2, 3, 4 ] print(data.strip())
Traceback (most recent call last): File "main.py", line 2, in print(data.strip()) AttributeError: 'list' object has no attribute 'strip'
# strip() with character parameter string = ' ....aaaToppr learningaa.... ' # Leading and trailing whitespaces are not removed print(string.strip('.a')) # All , ., a characters in the left and right of string are removed print(string.strip(' .a'))
....aaaToppr learningaa.... Toppr learning
Notice in the first strip() function, we have passed the character parameter as ‘.a’. As there are whitespaces included in the string variable, the argument passed and the string does not match. Hence, no character will be removed.
In the second strip() function, we have passed ‘ .a’ as our character argument. This argument contains a space, dot(.), and ‘a’ character which is present in the original string variable. Hence, they will be removed from the leading and trailing positions of the string.
string = 'malayalam' # Leading and trailing whitespaces are removed print(string.strip('ma'))
Frequently Asked Questions
Q1. What is strip() in Python?
Answer. Python provides a built-in strip() function which is used to remove the unwanted leading and trailing whitespaces and specified characters from the string. This function is used only on variables containing string type of data. The syntax for strip() function is as follows –
Q2. Why do we use the strip() function in Python?
Answer. Python strip() function can be used to remove multiple occurrences of a character or characters which are mistakenly added. The strip() function removes leading and trailing whitespaces whereas the strip([characters]) eliminates leading and trailing characters passed to the strip() function.
str = '1111Hello1111' print(str.strip('1'))
The above code snippet will remove all the leading and trailing 1s and will return the string as ‘Hello’ only.
Q3. How do you strip a list in Python?
Answer. The python strip() function is only limited to function with string format of data. If you use it for a list, the compiler will give an error. For example –
my_list = ['a', 'b', 'c', 'd'] print(my_list.strip())
Traceback (most recent call last): File "main.py", line 2, in print(my_list.strip()) AttributeError: 'list' object has no attribute 'strip'
Q4. How do you strip text in Python?
Answer. There are 2 methods to strip text in Python. The first method is without specifying any arguments, i.e., str.strip() which will remove the leading and trailing whitespaces. The second method is by specifying the character arguments, i.e., str.strip([chars]) which will remove leading and trailing characters only if they are present in the original string.