Python String lstrip() method
Python String lstrip() method returns a copy of the string with leading characters removed (based on the string argument passed). If no argument is passed, it removes leading spaces.
Syntax:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
string.lstrip(characters)
Parameters:
- characters [optional]: A set of characters to remove as leading characters.
Returns:
Returns a copy of the string with leading characters stripped.
Example 1
Python3
# Python3 program to demonstrate the use of # lstrip() method using default parameter # string which is to be stripped string = " geeksforgeeks" # Removes spaces from left. print(string.lstrip()) |
Output:
geeksforgeeks
Example 2
Python3
# Python3 program to demonstrate the use of # lstrip() method using optional parameters # string which is to be stripped string = "++++x...y!!z* geeksforgeeks" # Removes given set of characters from left. print(string.lstrip("+.!*xyz")) |
Output:
geeksforgeeks
Example 3
Python3
# string which is to be stripped string = "geeks for geeks" # Argument doesn't contain leading 'g' # So, no characters are removed print(string.lstrip('ge')) |
Output:
ks for geeks
Example 4
There is a runtime error when we try to strip anything except a string.
Python3
# Python3 program to demonstrate the use of # strip() method error string = " geeks for geeks "list =[1, 2, 3] # prints the error message print(list.lstrip()) |
Output:
print(list.lstrip()) AttributeError: 'list' object has no attribute 'lstrip'

