Python .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.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours