Python String | lstrip() method

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: 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.

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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())  

chevron_right


Output:

geeksforgeeks

 
Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"))  

chevron_right


Output:

geeksforgeeks

 
Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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'))

chevron_right


Output:

ks for geeks

 
Code #4: Runtime Error

There is a runtime error when we try to strip anything except a string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()) 

chevron_right


Output:

print(list.lstrip()) 
AttributeError: 'list' object has no attribute 'lstrip'


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.