English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python string lstrip() usage and example

Python string methods

lstrip() method is used to remove the specified characters from the beginning of the string, the default character is all whitespace characters, including spaces, newlines (\n), tabs (\t), and so on.

lstrip() removes characters from the left side based on the parameter (a string specifying the set of characters to be removed).

The syntax of lstrip() is:

string.lstrip([chars])

lstrip() parameters

  • chars (optional)-A string that specifies the set of characters to be removed.

If chars is not provided as a parameter, all leading spaces will be removed from the string.

lstrip() return value 

lstrip() returns a copy of the string with the leading characters removed.

chars remove all combinations of characters from the left side of the string until the first mismatch.

Example: lstrip() operation

random_string = '   this is good '
# Leading spaces have been deleted
print(random_string.lstrip())
# The parameter does not contain spaces
# No characters were deleted.
print(random_string.lstrip('sti'))
print(random_string.lstrip('s ti'))
website = 'https://www.oldtoolbag.com/'
print(website.lstrip('htps:'))/.'))

When running the program, the output is:

this is good 
   this is good 
his is good 
www.oldtoolbag.com/

Python string methods