Python Tutorials

Python String lstrip() Method

Syntax:

str.lstrip('characters')

'characters' is optional


Let's see with Examples:

str = 'Welcome to getechready'
new_str = str.lstrip('Wel') #the argument is case senstitve
print(new_str)

Output:

>>>come to getechready


  • Removing the leading spaces :
  • str = '  Welcome to getechready'
    new_str = str.lstrip()
    print(new_str)


    Output:

    >>>Welcome to getechready


  • Similar to strip() method the order and number of the characters passed in the lstrip() method does not matter.
  • str = '   mississippi river'
    new_str = str.lstrip('is m')
    print(new_str)


    Output:

    >>>ppi river

    As you can see the space can be provided at any position, it is not mandatory to provide space at first position only. Let's see if we do not provide space


    str = '   mississippi river'
    new_str = str.lstrip('ism')
    print(new_str)


    Output:

    >>>   mississippi river

    Nothing is removed as lstrip() was expecting a space also to match the starting characters.


    Also read: strip() method and rstrip() method