DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

4 1

Python String lstrip()

The Python String lstrip() method is a built-in function that strips leading characters based on the arguments passed to the function and returns the copy of a string.

Also read How to Fix TypeError: list indices must be integers or slices, not str

In this article, we will learn about the Python String lstrip() method with the help of examples.

lstrip() Syntax

The Syntax of lstrip() method is:

string.lstrip([chars])
Enter fullscreen mode Exit fullscreen mode

lstrip() Parameters

The lstrip() method takes one parameter, and it’s optional.

  • chars(optional) – set of characters representing string that needs to be removed from the left-hand side of the string.

If the chars argument is not passed, the lstrip() function will strip whitespaces at the start of the string.

lstrip() Return Value

The lstrip() method returns a copy of the string by stripping the leading characters based on the arguments passed.

Note:

  • If we do not pass any arguments to lstrip() function, by default, all leading whitespaces are truncated from a string.
  • If the string does not have any whitespaces at the beginning, the string will be returned as-is, matching the original string.
  • If the characters passed in the arguments do not match the characters at the beginning of the string, it will stop removing the leading characters.

Example 1: Working of lstrip()

# Only leading whitespaces are removed
text1 = ' Python Programming '
print(text1.lstrip())

# Remove the whitespace and specified character at
# leading end
text2 = ' code its my code '
print(text2.lstrip(' code'))

# Remove the specified character at 
# leading end
text3 = 'code its my code'
print(text3.lstrip('code'))

Python Programming   
its my code        
 its my code
Enter fullscreen mode Exit fullscreen mode

Example 2 – How to use lstrip() method in real world?

In the below example, we have a list of the price in dollars. However the dollar sign is appended at both the trailing and leading end of each element. We can simply iterate the list and remove the dollar symbol at the left hand side using the lstrip() method as shown below.

price = ['$100$', '$200$', '$300$', '$400$', '$500$']
new_price = []
for l in price:
    new_price.append(l.lstrip('$'))

print(new_price)

Enter fullscreen mode Exit fullscreen mode

Output

['100$', '200$', '300$', '400$', '500$']
Enter fullscreen mode Exit fullscreen mode

The Fastest, Most Accurate API for Voice AI

Ad Image

Building an AI Agent that needs to deliver human-like conversations? | Speechmatics’ real-time ASR is available in 50 languages and can understand speech in a fraction of a second.

Try Free

Top comments (0)

AWS Security LIVE! Stream

Level up your security knowledge and skills

Join AWS Security LIVE! for expert insights on threat detection, infrastructure protection, and more. Hosted by AWS experts and AWS Partners. No sales pitches, just deep dives!

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay