formatting

str.lstrip

Returns a copy of the string with leading characters removed. Defaults to whitespace; a chars argument specifies a set of characters to strip from the left.

str.lstrip([chars])

Parameters

Parameter Purpose
chars character set to strip from the left; default is whitespace
returns new str trimmed on the left (original unchanged — str is immutable)

Examples

>>> '   hello   '.lstrip()
'hello   '

only leading whitespace is removed

>>> '000123'.lstrip('0')
'123'

chars set removes any of those characters from the left

>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

removes any leading char in the set (a common gotcha)

>>> '\n\tspace'.lstrip()
'space'

tabs and newlines count

Gotcha

chars is a set of characters, not a prefix. Since 3.9 use str.removeprefix() to strip a literal prefix string.

Related methods

← All Python string methods · Python built-ins