Python String lstrip() method
Last Updated :
22 Aug, 2022
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.
Python String lstrip() Method Syntax:
Syntax: string.lstrip(characters)
Parameters:
- characters [optional]: A set of characters to remove as leading characters. By default, it uses space as the character to remove.
Return: Returns a copy of the string with leading characters stripped.
Python String lstrip() Method Example:
Python3
string = " geeksforgeeks"
print (string.lstrip())
|
Output:
geeksforgeeks
Example 1: Program to demonstrate the use of lstrip() method using multiple characters
Python3
string = "++++x...y!!z* geeksforgeeks"
print (string.lstrip( "+.!*xyz" ))
|
Output:
geeksforgeeks
Example 2:
Python3
string = "geeks for geeks"
print (string.lstrip( 'ge' ))
|
Output:
ks for geeks
Exception while using Python String lstrip() Method
There is an AttributeError when we try to use lstrip() method on any other data-type other than Python String.
Python3
nums = [ 1 , 2 , 3 ]
print (nums.lstrip())
|
Output:
print(list.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...