Python lstrip

The Python lstrip method is useful to remove the specified characters from the Left-hand side of a string. By default, the lstrip function deletes white spaces and returns a new string. The syntax of the Python lstrip function is

String_Value.lstrip(Chars)
  • String_Value: A valid String literal.
  • Chars: This parameter is optional, and Omit this means the lstrip function considers the white spaces as a default parameter. To change the default white space, specify the Characters to strip from the Left-hand side of a string in Python.

Python lstrip method Example

The following set of examples helps to know the lstrip Function.

# Python lStrip Method Example

Str1 = '  Tutorial Gateway'

Str2 = Str1.lstrip()
print('Stripping Whitespaces on Left using lStrip() is =', Str2)

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

# Performing lStrip() function directly
Str3 = '00000000Tutorial Gateway00000000'.lstrip('0')
print("Stripping 0's on Left using lStrip() is =", Str3)

# Stripping Left Side
Str4 = '+++++*********Tutorial Gateway'.lstrip('+*')
print('Stripping + and * on Left using lStrip() is =', Str4)

Here, we declared a String variable Str1 and assigned a text value

Str1 = '  Tutorial Gateway'

It removes the empty spaces from the Left-hand side of String variable Str1 using lStrip function and prints the output.

Str2 = Str1.lstrip()
print('Stripping Whitespaces on Left using LStrip() is =', Str2)

The lStrip function in Python returns the output in a new string.

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

To modify the original String, write this lstrip statement

Str1 = Str1.lstrip()

The Python lstrip function only removes the specified characters from the left side of a string and omit right-hand side characters. Within the next String function statements, we have zeros on both sides. Moreover, see the above image that the lstrip function output is removing zeros from the Left-hand side only.

Str3 = '00000000Tutorial Gateway00000000'.lstrip('0')
print("Stripping 0's on Left using LStrip() is =", Str3)

In this Python statement, we used two characters to strip (+ and *) from the Left-hand side.

Str4 = '+++++*********Tutorial Gateway'.lstrip('+*')
print('Stripping + and * on Left using LStrip() is =', Str4)