Sunday, September 7, 2025
HomeLanguagesPython String lstrip() method

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.

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 = "   neveropen" 
# Removes spaces from left.
print(string.lstrip())


Output:

neveropen

Example 1: Program to demonstrate the use of lstrip() method using multiple characters

Python3




# string which is to be stripped
string = "++++x...y!!z* neveropen"
 
# Removes given set of characters from left.
print(string.lstrip("+.!*xyz"))


Output: 

neveropen

Example 2:

Python3




# string which is to be stripped
string = "Lazyroar for Lazyroar"
 
# Argument doesn't contain leading 'g'
# So, no characters are removed
print(string.lstrip('ge'))


Output: 

ks for Lazyroar

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]
# prints the error message
print(nums.lstrip())


Output: 

print(list.lstrip()) 
AttributeError: 'list' object has no attribute 'lstrip'
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11807 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11870 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS