Python String lstrip Method - Python Tutorial | Logic Practice
Python Course / Other / Python String lstrip Method

Python String lstrip Method

BLUF: This lesson on Python String lstrip Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String lstrip Method

Mastering Python String lstrip Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience