Advertisement



< Prev
Next >



Python - String lstrip() Method




The lstrip() string method is used to trim all the white space characters present at the left of the string i.e. leading white spaces in a string.

Note : None of the string methods change the original string and only return a new string which reflects the change performed by the string method, hence the returned string must be stored or used(if needed to reflect the result), otherwise it is lost.



Signature of lstrip()


The lstrip() method has no parameters, so we don't have to worry about passing it some value.

# Signature of lstrip() method 
lstrip()




Method lstrip() example


Here in program below, we have a string object that contains a string value with many white spaces at the beginning(left side) and we are trying to strip all these leading white spaces it by calling lstrip() method on it.

# Python -  Program to strip the leading blank spaces from a string using lstrip() method

# Creating a string
s1 = '         hello'

# Printing the original string
print('The value of string : ',  s1)


# Calling  the lstrip() method on string object 
print('The stripped value of string : ', s1.lstrip())


Output is :



The value of string :           hello
The stripped value of string :  hello





Method lstrip() does not modify the original content in a String object


The method lstrip() only returns the content of string object, minus the leading white spaces in it. Method lstrip() does not modify the original content of a String object. Let's understand this by an example.


Here in program below, we are trying to remove all the leading white space characters present in a string by calling the lstrip() method.
#Python - Method lstrip() does not remove the invoked string



# Creating a string
s1 = '       Deal with one difficult     task at a time'

# Printing the original string
print('The value of string : ',  s1)


# Calling  the lstrip() method on string object 
print('The stripped value of string : ', s1.lstrip())


# Printing the original string after calling lstrip() method on it.
print('The value of string value : ', s1)


Output is :


The value of string :         Deal with one difficult     task at a time
The stripped value of string :  Deal with one difficult     task at a time
The value of string value :         Deal with one difficult     task at a time

The string object contains the same original value even after the method lstrip() was called on it, because the method lstrip() only returns the content of string object, minus the leading white spaces in it. It does not modify the original content of a string object.


Advertisement




Strings are immutable but reference variables are mutable.


String objects are immutable, but the reference variables pointing to them are not, therefore in order to display the result of calling the lstrip() method on the original object, we can make the reference variable to the original string, point to the trimmed string value returned by the lstrip() method(but in this case, the original string will be lost).

#Python - Another example of lstrip() method


# Creating a string
s1 = '       mother nature provides for everyone     '

# Printing the original string
print('The value of string : ',  s1)


# Result of calling lstrip() is made to reference by the reference of original string.
s1 = s1.lstrip()


# Old reference variable pointing to the new string
print('Stripped value of the string  : ', s1)


Output is :


The value of string :         mother nature provides for     everyone     
Stripped value of the string  :  mother nature provides for     everyone   





Please share this article -





< Prev
Next >
< String strip() Method
String rstrip() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement