Last Updated : 12 Nov, 2024
The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string.
Let's take an example to remove whitespace from the starting of a string.
Python
s = " Hello Python!"
res = s.lstrip()
print(res)
Syntax of lstrip() Method
s.lstrip(chars)
s = " Hello Python! "
res = s.lstrip()
print(res)
Remove Custom Characters
If we have a string with various characters that we want to remove from starting of the string then we can use lstrip().
Python
s = ' ##*#Hello Python!#**## '
# removes all occurrences of '#', '*', and ' '
# from the end of string
res = s.lstrip('#* ')
print(res)
Hello Python!#**##
Notes:
We can also remove the leading newline characters (\n) from a string.
Python
s = '\nHello Python!\n'
# Removing newline characters
# from the end of string
res = s.lstrip()
print(res)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4