In Python, the lstrip()
method is used to remove leading whitespace characters (spaces, tabs, newlines) from a string. It returns a new string with the leading whitespace characters removed. The original string remains unchanged.
The syntax of the lstrip()
method is as follows:
string.lstrip([chars])
Here, string
is the original string, and chars
is an optional parameter that specifies the set of characters to be removed from the beginning of the string. If not provided, it will remove all leading whitespace characters.
Let's see some examples:
lstrip()
to remove leading whitespace characters
text = " Hello, world!"
stripped_text = text.lstrip()
print(stripped_text) # Output: "Hello, world!"
lstrip()
with specific characters to remove
text = "-----Hello, world!"
stripped_text = text.lstrip('-')
print(stripped_text) # Output: "Hello, world!"
lstrip()
with multiple characters to remove
text = "/*--*Hello, world!"
stripped_text = text.lstrip('*/-')
print(stripped_text) # Output: "Hello, world!"
In the last two examples, the lstrip()
method removes the specified characters from the beginning of the string, so the resulting string contains only the actual text.