Python String lstrip Method

Python lstrip

Python String lstrip Method

The Python lstrip function serves the purpose of eliminating all leading characters from a given string. It accepts an optional parameter of type char. When this parameter is not specified, the function defaults to removing all leading whitespace characters from the string.

Syntax of Python String Istrip Method

It has the following syntax:

Example

lstrip([chars])

Parameters

chars (optional): A list of chars.

Return

It returns a string.

Different Examples for Python String lstrip Method

Let’s examine a few instances of the lstrip method to grasp its functionality more effectively.

Example 1

A straightforward example to illustrate the functionality of the lstrip method. Observe how it eliminates any spaces that appear at the beginning of a string.

Example

# Python lstrip() method example

# Variable declaration

str =  "  C# Tutorial  "

# Calling function

str2 = str.lstrip()

# Displaying result

print(str)

print(str2)

Output:

Output

C# Tutorial

Example

Example 2

In this section, we will incorporate certain special characters into the string and utilize the lstrip method to eliminate any leading characters.

Example

# Python lstrip() method example

# Variable declaration

str =  "$$$$-C# Tutorial-$$$$"

# Calling function

str2 = str.lstrip('$')

# Displaying result

print(str)

print(str2)

Output:

Output

$$$$-C# Tutorial-$$$$

-C# Tutorial-$$$$

Example 3

To illustrate the functionality of the Python String Istrip method, let us consider an example.

Example

# Python lstrip() method example

# Variable declaration

str =  "http://www.logic-practice.com"

# Calling function

str2 = str.lstrip('http://w.')

# Displaying result

print(str)

print(str2)

Output:

Output

http://www.logic-practice.com

logic-practice.com

Input Required

This code uses input(). Please provide values below: