How Can We Help?
In this tutorial, we will explain how to use Python string lstrip() method with basic syntax by example for better understanding.
The String lstrip() method or String left Strip Method removes particular characters or space of a string from left direction.
Syntax:
<string_value/string_variable>.lstrip(strip character)
OR
<string_value/string_variable>.lstrip()
Input Parameters:
- string_value / String variable : The string that we want to remove a particular character from the left hand side.
- Strip Character : The character you want to eliminate from the string.
String lstrip Special Character
We Can Strip or remove special characters from the left-hand side of a string by giving a special character argument to the lstrip function. ex: string.lstrip(‘$’).
Example (1)
# The String Value That We Want To Perform Strip Against
str1='$$$python36$$'
#Print String After Strip $ Character from it
print('left Strip $: ',str1.lstrip('$'))
Output of example (1)
left Strip $: $$$python36
In the above example, ‘$’ is existent at the start and the end in the string value of string variable ‘str1’, and we have removed only those special characters from the left-hand side of a string by using str1.lstrip(‘$’) in the print statement.
String lstrip Whitespace
We Can Strip or remove the white spaces from the left-hand side of a string by calling the lstrip function without passing any argument. ex: string.lstrip().
Example (2)
# The String Value That We Want To Perform Strip Against
str2=' $$$python36$$ '
#Print String After removing whitespace from it
print('left Strip:',str2.lstrip())
Output of example (2)
left Strip $: $$$python36
In the above example, white spaces are existent at the start and the end in the string value of string variable ‘str1’, and we have removed only those white spaces from the left-hand side of a string by using string.lstrip() in the print statement.
String lstrip Newlines
We Can Strip or remove newlines from the left-hand side of a string by giving a new line symbol an argument to the left strip function. ex: string.lstrip(‘\n’).
Example (3)
# The String Value That Contains newlines before and after the string
str3='\n$$$python36$$\n'
#Print String After removing newlines from it
print('str3 value after removing new line:',str3.strip('\n'))
Output of example (3)
Strip: $$$python36$$
In the above example, Newlines are existent before and after the string value of string variable ‘str1’, and we have removed the new line that occurs on the left-hand side of a string by specifying the argument ‘\n’ , which is referring to a new line to left strip function in the print statement.
String lstrip Quotes
We Can Strip or remove quotes from only the left-hand side of a string by giving a quotation mark as an argument to the left strip function. ex: string.lstrip(‘”‘).
Example (4)
# The String Value That Contains quotes before and after the string value
str4='"Learn Python With Oraask"'
#Print String After removing quotes on the left side
print('str4 value after removing quotes:',str4.lstrip('"'))
Output of example (4)
str4 value after removing quotes: “Learn Python With Oraask
In the above example, quotation marks are existent before and after the string value of string variable ‘str1’. We have removed them from the left-hand side of a string by specifying the argument ‘“‘ to strip function in the print statement.
In this tutorial, you have learned the string lstrip() method in Python. What is it? And how to remove a special characters, whitespaces, quotes and newlines from only the left hand side of a string.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Formatting Methods:
- title() : It returns the “title cased” version of string means all the words start with uppercase and the remaining are lowercase.
- capitalize() : It capitalizes the first character of the String.
- center() : It returns a space-padded string with the original string centered to a total of width columns.
- swapcase() : It inverts the case of all the characters in a string.
- upper() : It converts all the characters of a string to uppercase.
- lower() : It converts all the characters of a string to lowercase.
- strip() : It removes particular characters of a string from left and left side.
- rstrip() : It removes particular character of a string from right side.
- ljust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- rjust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- zfill() : It returns an original string value with filled zero at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.