Python Glossary Русский

all Python 3Strings (str)function

str.lstrip

stripping

Return a copy with leading characters removed (whitespace by default).

Syntax

s.lstrip([chars])

The argument is a set of characters, not a prefix: lstrip keeps eating characters from the left as long as each one belongs to that set. That is why 'template.py'.lstrip('temp') gives 'late.py' and not '.py'. When you mean a whole prefix, use str.removeprefix() (Python 3.9+).

Examples

print('  hi'.lstrip())     # → hi
print('xxhi'.lstrip('x'))  # → hi
print('--a--'.lstrip('-'))  # → a--

See also

Open in the full glossarydocs.python.orgReport an inaccuracy