Python String lstrip() Method
The lstrip() method is used to truncate spaces or specified characters from the left side of a string.
Python String lstrip() Syntax
lstrip() method syntax:
str.lstrip([chars])
Python String lstrip() Parameters
- chars – Specifies the characters to be intercepted.
Python String lstrip() Return Value
Returns the new string generated by truncating the left space or the specified character of the string.
Python String lstrip() Example#1
The following example demonstrates the use of lstrip().
#!/usr/bin/python3
string = "++++x...y!!z* apidemos"
# Removes given set of characters from left.
print(string.lstrip("+.!*xyz"))
Output:
Python String lstrip() Example#2
The following example demonstrates an exception when using the Python string lstrip() method. AttributeError occurs when we try to use the lstrip() method on any data type other than Python strings.
#!/usr/bin/python3
nums = [1, 2, 3]
print(nums.lstrip())
Output: