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

Python Basic Tutorial

Python Flow Control

Fonctions en Python

Types de données en Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python string lstrip() usage and examples

Python string methods

lstrip() 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() parameter

  • 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 leading characters removed.

chars remove all combinations of characters from the left side of the string until the first non-matching.

Example: the working of lstrip()

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://fr.oldtoolbag.com/'
print(website.lstrip('htps:',/.'))

When running the program, the output is:

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

Python string methods