Python String lstrip()

Python String.lstrip() is used to strip/trim specified characters from the left side of this string. lstrip() method returns a new resulting string and does not modify the original string.

In this tutorial, we will learn the syntax and examples for lstrip() method of String class.

Syntax

The syntax to call lstrip() method on string x in Python is

</>
Copy
x.lstrip(characters)

where

ParameterRequired / OptionalDescription
charactersOptionalA string. Any of these characters that are present on the left side of this string shall be trimmed.
The default value is all whitespace characters.

Examples

In the following program, we take a string '..@hello world@', and trim the characters '.@' on the left side of the string.

Example.py

</>
Copy
x = '..@hello world@'
result = x.lstrip('.@')
print('Original String : ', x)
print('Stripped String : ', result)

Output

Original String :  ..@hello world@
Stripped String :  hello world@

If you observe, there is @ character on the right side of the string. Since, this character is not at left side, lstrip() does not trim this character.

Now, let us take a string with spaces on the left and right side, and trim this string using lstrip() method, with no arguments passed. Since the default value of characters parameter is whitespaces, lstrip() trims the whitespace characters on the left side of this string.

Example.py

</>
Copy
x = '     \thello world  '
result = x.lstrip()
print('Original String : ', x)
print('Stripped String : ', result)

Output

Original String :               hello world  
Stripped String :  hello world

Conclusion

In this Python Tutorial, we learned how to trim specified characters from the left side of the string using lstrip() method, with examples.