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.

Example: Strip whitespace from the left end

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

Input:

   Hello, World!   

Stripped:

   Hello, World!   

Output:

Hello, World!   

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

Example: Strip newline ‘\n’, tab ‘\t’ and carriage return ‘\r’

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

Input:

 \t\n\r Hello, World!    

Stripped:

 \t\n\r Hello, World!    

Output:

Hello, World!    

Strip Characters

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

Example: Strip single character ‘a’

S = 'aaaaab'
x = S.lstrip('a')
print(x)

Input:

aaaaab

Stripped:

aaaaab

Output:

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 ‘.’

Example: Strip multiple characters

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

Input:

http://www.example.com

Stripped:

http://www.example.com

Output:

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.

Example:

S = 'xxxxSxxxxSxxxx'
x = S.lstrip('x')
print(x)

Input:

xxxxSxxxxSxxxx

Stripped:

xxxxSxxxxSxxxx

Output:

SxxxxSxxxx
Python lstrip Method Example Explanation

Here is another example:

Example:

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

Input:

... - Version 3.2 Model-32

Stripped:

... - Version 3.2 Model-32

Output:

Version 3.2 Model-32