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:
# 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()) |
geeksforgeeks
Code #2:
# 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" )) |
geeksforgeeks
Code #3:
# 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' )) |
ks for geeks
Code #4: Runtime Error
There is a runtime error when we try to strip anything except a string.
# 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'
Recommended Posts:
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- numpy string operations | lstrip() function
- Python | Pandas Series.str.strip(), lstrip() and rstrip()
- Python String | islower() method
- Python String | isupper() method
- Python String Title method
- Python string | capwords() method
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Merge Tuple String List values to String
- Python | Sorting string using order defined by another string
- Python | Check if string ends with any string in given list
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Sort each String in String list
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.