Python String lstrip() Method

Strips characters from the left end of a string

Usage

The lstrip() method removes whitespace from the beginning (leading) of the string by default.

By adding chars parameter, you can also specify the characters you want to strip.

Syntax

string.lstrip(chars)

Python string lstrip() method parameters
ParameterConditionDescription
charsoptionalA list of characters to be removed from the string

Return Value

The method return a copy of the string with the specified characters removed from the beginning of the string.

Strip Whitespace

By default, the method removes leading whitespace.

S = '   Hello, World!   '
x = S.lstrip()
print(x)
# Prints Hello, World!   

Newline '\n', tab '\t' and carriage return '\r' are also considered as whitespace characters.

S = ' \t\n\r Hello, World!    '
x = S.lstrip()
print(x)
# Prints Hello, World!    

Strip Characters

By adding chars parameter, you can also specify the character you want to strip.

# Strip single character 'a'
S = 'aaaaab'
x = S.lstrip('a')
print(x)
# Prints b

Strip Multiple Characters

The chars parameter is not a prefix; rather, all combinations of its values are stripped.

In below example, strip() would strip all the characters provided in the argument i.e. ‘h’, ‘w’, ‘t’, ‘p’, ‘:’, ‘/’ and ‘.’

S = 'http://www.example.com'
x = S.lstrip('hwtp:/.')
print(x)
# Prints example.com

More About lstrip() Method

Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars.

S = 'xxxxSxxxxSxxxx'
x = S.lstrip('x')
print(x)
# Prints SxxxxSxxxx

Here is another example:

S = '... - Version 3.2 Model-32'
x = S.lstrip('.- ')
print(x)
# Prints Version 3.2 Model-32