Introduction to strip Function in Python
Python strip function is used for stripping a string in such a manner that the leading and trailing characters can be eliminated. It is a built-in function in python. After the principal and rambling characters are removed off, a new string will be returned in this process.
Syntax :
string.strip([chars])
The format of characters which we need to strip can be decided in the strip function. When anything additional than white spaces has to be removed, then they can be mentioned in the ‘chars’ position. Precisely to state, the characters which need to be detached off are placed in the ‘chars’ position of the above syntax. The reserved word strip is used to represent the function strip. This stripping function falls under the default library. Hence it can be called without means of any library.
How Does Python strip Function Work?
The strip function can grasp numerous arguments inside it. When no arguments are passed as a part of the strip function, then the spaces which lead and trail the character string will be removed and returned by the strip function. As like above, when the character match for stripping matches the given string, then the specified characters will be removed from the shared string. In the case when no matches are found then, no changes will be applied to the string. When the character string mentioned in the strip function does to match with the string to be stripped in the left, then removing leading characters in the left of the string is stopped. Similarly, When the character string mentioned in the strip function does to match with the string to be stripped in the right, then removing leading characters in the right of the string is stopped.
Additionally, when the stripping process needs to be taken place on only one side of the given string, then two different methods are used for this purpose. They are the String.rstrip() and String.lstrip() methods. The rstrip() method is used to strip a given character set at the right of the string, whereas the lstrip() method is used to strip a word to the left of the given string.
Examples of strip() Function in Python
Below are the examples mentioned :
Example #1
The below example is used to strip a given string.
Code:
string_before_strip = '00000000abcdefghijklmnopqrstuvwxyz00000000'
print( " String before striping : " , string_before_strip)
string_after_strip = string_before_strip.strip( '00000000' )
print( " String after striping : " , string_after_strip)
Output:
Explanation: In the above program, a string value that is trailing and leading with zeroes is used as a sample. The set of zeroes ‘00000000’ are passed as the strip off value for the string. The string value before and after the stripping process is printed in the console. From the derived output, it can be clearly noticed that all the leading and trailing zeroes have been successfully removed, and the exact actual string is alone printed in the console.
Example #2
The below example is used to strip a given string.
Code:
string_before_strip = '00000000abcdefghijklmnopqrstuvwxyz00000000'
print( " String before striping : " , string_before_strip)
string_after_strip = string_before_strip.rstrip( '00000000' )
print( " String after striping : " , string_after_strip)
Output:
Explanation: Here, In this example, a string value with trailing and leading zeroes is used as a sample. The set of zeroes ‘00000000’ are passed as the strip off value for the string. The string value before and after the stripping process is printed in the console. Here instead of a normal strip() function, the rstrip() function is been used, which means to strip the trailing zeroes or the right-sided zeroes in the mentioned string. The same can be noticed in the output that all the trailing zeroes are successfully removed and the remaining string is printed.
Example #3
The below example is used to strip a given string.
Code:
string_before_strip = '00000000abcdefghijklmnopqrstuvwxyz00000000'
print( " String before striping : " , string_before_strip)
string_after_strip = string_before_strip.lstrip( '00000000' )
print( " String after striping : " , string_after_strip)
Output:
Explanation: Here, In this example, a string value that has trailing and leading zeroes is used as a sample. The set of zeroes ‘00000000’ are passed as the strip off value for the string. The string value before and after the stripping process is printed in the console. Here instead of a normal strip() function, the lstrip() function is been used, which means to strip the leading zeroes or the left-sided zeroes in the mentioned string. The same can be noticed in the output that all the left-sided zeroes are successfully removed, and the remaining string is printed.
Example #4
The below example is used to strip a given string.
Code:
string_before_strip = ' 00000000abcdefghijklmnopqrstuvwxyz00000000 '
print( " String before striping : " , string_before_strip)
string_after_strip = string_before_strip.strip()
print( " String after striping : " , string_after_strip)
Output:
Explanation: Here, In this example, a string value that has trailing and leading zeroes and on top of it leading and trailing spaces are also additionally used. No arguments are passed as strip off value for the string. The string value before and after the stripping process is printed in the console. As we could finely notice, the absence of arguments in the strip function actually removes the leading and trailing spaces from the mentioned string.
Example #5
The below example is used to strip a given string.
Code:
string_before_strip = '00000000abcdefghijklmnopqrstuvwxyz00000000'
print( " String before striping : " , string_before_strip)
string_after_strip = string_before_strip.strip('1234')
print( " String after striping : " , string_after_strip)
Output:
Explanation: Here, In this example, a string value that has trailing and leading zeroes and on top of it leading and trailing spaces are also additionally used. A set of integers are passed as the strip off value. The string value before and after the stripping process is printed in the console. Since the strip of value does not match the actual string value, no changes are applied to the input string.
Recommended Articles
This is a guide to strip() Function in Python. Here we discuss a brief overview of strip() Function in Python and its Examples and its Code Implementation. You can also go through our other suggested articles to learn more –