Open In App

Python String lstrip() Method

Last Updated : 12 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The lstrip() method removes leading whitespace characters from a string. We can also specify custom characters to remove from the beginning/starting of the string.

Let's take an example to remove whitespace from the starting of a string.

Python
s = "   Hello Python!"
res = s.lstrip()
print(res)

Output
Hello Python!

Syntax of lstrip() Method

s.lstrip(chars)

  • s: The input string
  • chars (optional): A set of characters to remove as trailing characters

Examples of lstrip() Method

Remove Trailing Whitespaces

Python
s = "   Hello Python!     "
res = s.lstrip()
print(res)

Output
Hello Python!     

Remove Custom Characters

If we have a string with various characters that we want to remove from starting of the string then we can use lstrip().

Python
s = '  ##*#Hello Python!#**##  '

# removes all occurrences of '#', '*', and ' ' 
# from the end of string
res = s.lstrip('#* ')
print(res)

Output
Hello Python!#**##  

Notes:

  • lstrip('#* ') removes any #, *, and spaces from the starting/beginning of string.
  • It stops stripping characters from the start of string once it encounters a character that are not in the specified set of characters.

Remove Newline Characters

We can also remove the leading newline characters (\n) from a string.

Python
s = '\nHello Python!\n'

# Removing newline characters
# from the end of string
res = s.lstrip()

print(res)

Output
Hello Python!


Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy