The Python lstrip function is useful for removing the white spaces or specified characters from the left-hand side of a string. By default (without a parameter), it deletes leading black spaces and returns a new string. However, we can use it to remove unwanted leading characters, spaces, newlines (\n), and tabs(\t).
Python lstrip() syntax
The syntax of the lstrip() function is
str.lstrip(Chars)
Here, str is the string where the stripping happens.
Parameters
Chars: This parameter is optional, and omitting this argument means it considers the white spaces as a default parameter. So, it strips white spaces from the left side of a string.
To change the default white space, please specify the characters to strip from the left-hand side.
Return Value
The lstrip() function returns a copy of an original string by stripping unwanted characters or spaces from the beginning of the string.
TIP: Use the rstrip() function to remove the trailing spaces. Otherwise, use the strip() function to remove unwanted characters from the start and end of the string.
How does Python lstrip() method work?
The lstrip() function starts looking for the given characters(s) from the beginning of the string and removes the matching characters until it encounters any other character not specified in the parameter.
For example, lstrip(‘*’) searches for the * symbol, and if it encounters any other character (apart from *), it stops deleting and prints the remaining text.
NOTE: It removes any combination of characters, not the characters in sequence.
Examples of Python lstrip() string method
The following set of examples helps to know the lstrip Function.
Str1 = ' Tutorial Gateway'
Str2 = Str1.lstrip()
print('Stripping Whitespaces on Left is =', Str2)
# Observe the Original String
print('Converted String is =', Str1.lstrip())
print('Original String is =', Str1)
# Performing directly
Str3 = '00000000Tutorial Gateway00000000'.lstrip('0')
print("Stripping 0's on Left is =", Str3)
# Stripping Left Side
Str4 = '+++++*********Tutorial Gateway'.lstrip('+*')
print('Stripping + and * on Left is =', Str4)

It removes the empty spaces from the left-hand side of the String variable Str1 using this function and prints the output.
Str2 = Str1.lstrip()
print('Stripping Whitespaces on Left using LStrip() is =', Str2)
By default, the lstrip function returns the output in a new string.
print('Converted String is =', Str1.lstrip())
print('Original String is =', Str1)
To modify the original text, write the following statement.
Str1 = Str1.lstrip()
The Python lstrip() function only removes the specified characters from the left side of a string and omits all the right-hand side characters.
Within the next String function statements, we have zeros on both sides. Moreover, see in the above image that the output removes 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)
Remove leading whitespaces
In the following example, we used the lstrip() function without any parameter value. When no parameter is passed, the lstrip() function removes whitespaces from the left side of the given text.
text = " Hi"
s = text.lstrip()
print(s)
Hi
NOTE: You can also use text.lstrip(‘ ‘) to remove the leading blank spaces.
Remove the Custom Character from the string’s left side
If we use the Python lstrip() function with a parameter, we can remove unwanted characters from the beginning of the string.
For instance, in a production environment, logs may have unwanted characters or prefixes. Here, we pass # parameter, and the lstrip() removes # symbols from the starting position.
msg = "##Error: Connection Failed!"
s = msg.lstrip('#')
print(s)
Error: Connection Failed!
Remove Multiple leading Characters
The Python lstrip() method also allows us to use multiple characters so that they are removed from the beginning of the string.
In the following example, the lstrip() function starts looking for @, #, and * symbols at the start of a string. If they are found, remove those special characters (Symbols). Once it finds a character other than these three, it stops the stripping process.
msg = "@####******New Year"
s = msg.lstrip('@#*')
print(s)
New Year
Remove the left side newline and tab characters
The lstrip() function also removes the leading newline (\n) and tab (\t) characters.
text = "\n\tBenz"
print(text)
s = text.lstrip('\n\t')
print(s)
Benz
Benz
Using the Python lstrip() function on a list of strings
The following example shows how to clean the data imported from the database. Here, the list has three users with leading blank spaces. The list comprehension with a for loop iterates over list items, and lstrip() removes leading whitespaces.
users = [' Mike', ' Tom', ' John']
cleanNames = [name.lstrip() for name in users]
print(cleanNames)
['Mike', 'Tom', 'John']
FAQ
Is the lstrip() method case-sensitive?
Yes. The lstrip() function treats lowercase and uppercase characters as different entities. Here, it removed T but left t as it is.
text = "TTTTTTTtt New Year"
s = text.lstrip('T')
print(s)
tt New Year
TIP: Use the string lower() function to convert the string to lowercase and apply the lstrip.
Does the Python lstrip() function modify the original string?
As strings are immutable, the lstrip() does not modify the original string. So, we must always assign the result to a new string to see the leading stripped text.
text = " BMW"
text.lstrip()
print(text)
s = text.lstrip()
print(s)
BMW
BMW
Can I use lstrip() to remove words?
Although we can use it, the lstrip() method is to remove characters, not a substring. The best option is using the removeprefix() function.