Python strip

Python strip string function is one of the Python String methods. This Python strip function removes the specified characters from both the Right-hand side and Left-hand side of a string (by default, White spaces) and returns a new string.

The Python strip function is the combination of Python rstrip and Python lstrip functions. Let us see the syntax of the Python strip string function is

String_Value.strip(Chars)
  • String_Value: A valid string literal.
  • Chars: This parameter is optional. If it omitted, the strip Function considers the white spaces as a default parameter. To change the default value, Please specify the Characters to strip from a string.

Python strip function Example

The following set of examples helps to know the strip Function in Python.

# Strip Method Example

Str1 = '  Tutorial Gateway  ';

Str2 = Str1.strip()
print('Stripping White spaces using Strip() is =', Str2)

# Observe the Original String
print('Converted String is =', Str1.strip())
print('Original String is =', Str1)

# Performing Strip() function directly
Str3 = '00000000Tutorial Gateway00000000'.strip('0')
print("Stripping 0's using Strip() is =", Str3)

Str4 = '+++++Tutorial Gateway****'.strip('+*')
print('Stripping + and * on both sides using Strip() is =', Str4)

# Stripping Left Side
Str5 = '***********Tutorial Gateway'.strip('*')
print('Stripping Left Side using Strip() is =', Str5)

# Stripping Right Side
Str6 = 'Tutorial Gateway========='.strip('=')
print('Stripping Right Side using Strip() is =', Str6)
Python Strip

Within this String function example, First, we declared the String variable Str1 and assigned a string with empty spaces on both sides.

Str1 = '  Tutorial Gateway  ';

It removes the white spaces from both the Left & Right-hand side of String variable Str1 using Strip function and prints the output.

Str2 = Str1.strip()
print('Stripping White spaces using Strip() is =', Str2)

The String strip function returns the output in a new string, instead of altering the original string.

print('Converted String is =', Str1.strip())
print('Original String is =', Str1)

From the above screenshot, see the original string Str1 is unchanged. To change the original String, write the following Python statement

Str1 = Str1.strip()

The Python strip function allows using parameters. Within the following statements, we have zeros on both sides, and we are passing ‘0’ as the argument to strip function, which means it removes zeros from both sides.

Str3 = '00000000Tutorial Gateway00000000'.strip('0')
print("Stripping 0's using Strip() is =", Str3)

Here, we used two characters to strip (+ and *)

Str4 = '+++++Tutorial Gateway****'.strip('+*')
print('Stripping + and * on both sides using Strip() is =', Str4)

It is stripping only the left-hand side of a given string using strip function (Acting like lstrip function).

Str5 = '***********Tutorial Gateway'.strip('*')
print('Stripping Left Side using Strip() is =', Str5)

The strip function was stripping only the Right-hand side of the string (Acting like rstrip function).

Str6 = 'Tutorial Gateway========='.strip('=')
print('Stripping Right Side using Strip() is =', Str6)

Please refer to Python rstrip and Python lstrip function articles.