Python strip string function removes the specified characters from both the Right-hand side and Left-hand side of a text (by default, White spaces), and this method returns a copy of the string as new.
This Python function is the combination of right and left strip string methods, and it removes the leading and trailing whitespace.
Let us see the syntax of the Python strip string function is shown below.
- String_Value: A valid argument.
- Chars: This parameter is optional. If it is omitted, it considers the white spaces as a default parameter. So, this method removes the leading and trailing spaces. To change the default value, Please specify the trailing and leading characters to remove from a string.
Python strip string function Example
The following set of examples helps to know this Python strip method. Within this method example, First, we declared the String variable Str1 and assigned a string with empty spaces on both sides.
The first Python statement removes the white spaces from both the Left & Right-hand sides of variable Str1 using the strip string function and prints the output.
From the next two statement output, you can observe that this method returns the output in a new, instead of altering the original. In the next line, we assigned the result to itself for changing the original in Python.
As we said earlier, the Python strip string function allows using parameters. Within the next two statements, we have zeros on both sides, and we are passing ‘0’ as the argument, which means it removes zeros from both sides. Next, we used two characters to remove (+ and *).
str5 code block removes only the lead characters or left-hand side of a given sentence using this method.
Str1 = ' Tutorial Gateway '; Str2 = Str1.strip() print('Remove White sapces using this is =', Str2) # Observe the Original print('Converted is =', Str1.strip()) print('Original is =', Str1) # Performing function directly Str3 = '00000000Tutorial Gateway00000000'.strip('0') print("Delete 0's using this is =", Str3) Str4 = '+++++Tutorial Gateway****'.strip('+*') print('Remove + and * on both using this is =', Str4) # Left Str5 = '***********Tutorial Gateway'.strip('*') print('Delete Left Side using this is =', Str5) # Right Str6 = 'Tutorial Gateway========='.strip('=') print('Remove Right Side using this is =', Str6)