Python String lstrip
Now, we are going to learn about the lstrip method. Using this method, we get a copy of the string, with the leading characters removed. We can provide a string as an argument, specifying the set of characters to be removed from the left of the string. If we do not specify the argument string, it defaults to removing the whitespaces. Let’s have a look at a simple example, which demonstrates the same thing.
Python String lstrip() Method
As you can see in the above program, we have a string, where we have some hyphen signs at the beginning and end of the string. Then, we are calling the lstrip method, and we specified the hyphen sign as the argument string, to be removed from the left of the string. So, in the output, you can see all the leading hyphen signs removed, and the copy of the string is returned. Let’s have a look at the output as well.
GyaniPandit—–
As you can see in the output, we got the string with the leading characters removed. So, as and when required, we can make use of the lstrip method, remember that this method returns the copy of the string, with the leading characters removed.
If we do not specify the string specifying the characters to be removed, then it defaults to remove the whitespace. Let’s have a look at a simple program, which tries to demonstrate the same thing.
As you can see in the above program, we have a string, with some leading and trailing whitespaces, and then we are calling the lstrip method on the string. If you try to execute the program, you can simply find that we get a string, with the leading whitespaces removed, since we had not specified the string argument. You can try to execute the program and observe the output.
So, as and when required, we can make use of the lstrip method in our programs. Remember that the method returns the string with the leading characters/whitespaces removed. The original string is still as it was.