Python String lstrip() Method

The lstrip() method returns a copy of the string by removing leading characters specified as an argument. By default, it removes leading whitespaces if no argument passed.

Leading characters occur at the start of the string (leftmost part of the string).

Syntax:

str.lstrip(characters)

Parameters:

characters: (optional) A char or string to be removed from the starting of the string.

Return Value:

A string.

The following examples demonstrates the lstrip() method.

Example: lstrip()
>>> mystr = '     Hello World     '
>>> mystr.lstrip() # removes leading spaces
'Hello World     '
>>> mystr = '''
Python is 
a programming language'''
>>> mystr.lstrip()  # removes leading \n
'Python is \na programming language'
>>> mystr = '----Hello World----'
>>> mystr.lstrip('-')  # removes leading -
'Hello World----'

You can specify one or more characters as a string to be removed from the string in any order, as shown below.

Example: lstrip()
>>> '#$2Hello World#$2'.lstrip('$2#')
'Hello World#$2'
>>> '#$-2Hello World#$2'.lstrip('$2#')
'-2Hello World#$2'
>>> 'www.tutorialsteacher.com'.lstrip('xabw.') # remove www.
'tutorialsteacher.com'
>>> 'ābc'.lstrip('ā') # removes Unicode char
'bc'

In the above example, '#$2Hello World#$2'.lstrip('$2#') removes leading chars'$', '2', or '#' if appears in any order. However, #$-2Hello World#$2'.lstrip('$2#') removes only leading '#' and '$', because '-' is not specified to be removed, so it stops there and consider it as a word.