Python lstrip() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The Python String lstrip() function produces a replica of the string without the initial few characters, depending on the string argument passed. It strips leading spaces when no argument is provided.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of Python lstrip() Function

'lstrip()' removes characters from the left side of the input string based on the string argument it receives, which specifies the characters to be removed.

The syntax of lstrip() is:

Parameters of Python lstrip() Function

The chars parameter, a string, specifies the characters that will be dropped from the clone of the string by the lstrip() function.

The chars parameter takes whitespace characters as default if you don't supply any argument or use None. The initial whitespace characters from the duplicate of the string will be eliminated in this situation by the lstrip() function.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Return Value of Python String lstrip() Function

Python lstrip() function returns a string with the initial characters removed.

Errors and Exceptions of Python lstrip() Function

When we attempt to use the lstrip() function on a data type other than a Python String, an AttributeError occurs.

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Uses of Python lstrip() Function

lstrip() function is used to just remove leading whitespace and characters. When the user wish to purge the string's beginning simply of whitespace and characters, this is useful.

Examples of Python lstrip() Function

Use lstrip() function to just trim initial whitespace and characters.

  • When no parameter is passed in the lstrip() function, it removes the leading whitespaces by default.

Output:

  • Removing specific characters from the beginning of the string.

"Sam" being cut from a string offers as an example of this. To demonstrate how lstrip() function works,  "Sam" characters in this example is used at both the beginning and the end of the string.

Output:

  • Removing multiple characters using lstrip() function

Output:

In the above example, the lstrip() function removes the given set of characters from the left of the input string.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

How Does the Python lstrip() Function Work?

  • The function looks for the given sequence or characters, at the start of the string and removes characters until it is halted by a distinct character that varies from the character/pattern passed.
  • When no arguments are supplied as a part of the function,  the spaces that lead the character string will be eliminated and returned by the lstrip function.
  • No modifications will be made to the string if no matches are identified.

FAQs

Q: Define "strip()" function in Python?

A: Python's strip() function is used to manipulate strings. It uses the syntax string.strip(). After deleting the spaces from the input string's left and right sides, it will return the altered string.

For example:

Output:

Q: Difference between "lstrip()" and "rstrip()"?

A: lstrip(): In the lstrip() function, "l" stands for left, as it eliminates the leading whitespace on the left of the string.

rstrip(): In the rstrip() function, "r" stands for right, as it eliminates the leading whitespace on the right of the string.

Conclusion

  • The lstrip() function of the Python string class is implemented to obtain a duplicate of a string without the beginning characters.
  • The lstrip() method in Python returns a string with the initial characters stripped.
  • The given characters will be stripped from the shared string when the character matches the given string.
  • The lstrip function stops removing leading characters from the left of the string when the character string specified there does not match the string that is to be stripped.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.