.lstrip()
Anonymous contributor
Published Oct 11, 2023
Contribute to Docs
The .lstrip()
method removes the leading characters from a string.
Syntax
string.lstrip(chars)
The .lstrip()
method removes the whitespace at the beginning of a string. The optional chars
parameter is a string for designating specific leading characters to be removed.
Examples
Unless chars
is specified, .lstrip()
removes the leading whitespace. If chars
is specified .lstrip()
will remove any leading characters in chars from the string. chars
can be in any order.
hi = ' Hi there'print(hi.lstrip())filePath = 'cd ./user/home'print(filePath.lstrip('cd ./'))example = 'Hi, Welcome to Docs'example = example.lstrip('hiHI, ')print(example)
This code will result in the following output:
Hi thereuser/homeWelcome to Docs
Note:
.lstrip()
returns a new string and does not modify the original string.
Codebyte Example
The following code is runnable and uses the .lstrip()
method.
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.