Python strip is a built-in function that returns the string without white space if there any at the start or the end. If you pass a string that has white spaces at the end, the strip function will return the string without white spaces. If there are no white spaces in the string, then the function will return the string as it is.
Syntax of python strip
The parameter passed to the strip function is an optional parameter. If you want, you can pass it or ignore it.
How to use the python strip function?
Since it is a string function, we can only use this function on a string. There are many ways to use the strip function. We will discuss one by one below.
Directly using python strip function in the print function.
print(' Pawan Misal '.strip())
In the above example, we can see the white spaces at the starting and end of the string are removed by the strip function.
Storing a string a variable and use the strip function of the variable.
str = ' hello world ' print(str.strip())
In this example, we are storing the string in a variable and using the strip function on it. We will understand this example in detail.
str = ' hello world ' print('the number of characters in the string is:',len(str)) result = len(str.strip()) print('After using strip() the number of characters in the string is:', result)
the number of characters in the string is: 20 After using strip() the number of characters in the string is: 11
You can see the number of characters present in the string before using the strip() are 20, and after using the strip(), the character count is 11. That means the strip() python function removers white spaces at the start and the end of the string. It does not remove the spaces in between the string.
Passing the character as a parameter to the strip function.
str = '00000hello world00000' result = str.strip('0') print(result)
In this example, 0 is passed as a parameter to the strip function. So strip function returns the string without 0.
str = ' 00000hello world00000 ' result = str.strip(' 0') print(result)
In the above example, String has white spaces and 0 in it. So we pass a white space and 0 as a parameter to the strip function. The result of the strip function is in front of you.
If the characters passed as a parameter are not present in the string, the strip function would return the string as it is.
str = 'hello world' result = str.strip('xyz') print(result)
In this example, ‘xyz’ is passed to the function which is not present in the string. So the function strip python returns the string as it is.
Python strip function removes the white space at the leading edge of the string and the string’s trailing edge and returns it.
Pass the punctuation symbol as a parameter to the function. The strip function will return the string without punctuation.
str = ‘hello world…’
result = str.strip(‘.’)
Python provides rstrip function to remove the newline from the string.
str = ‘hello world. \n’
result = str.rstrip()
It is a built-in string function provided by python to remove the leading and trailing white spaces in the string.