Thursday, October 1, 2015

Python Tutorial: Python Lstrip String Method #65





T0 learn more about the Python Lstrip String Method visit our website http://learnpythontutorial.com/python-lstrip-string-method/



Python Lstrip String Method

In this Python tutorial, we will explore the Python lstrip string method. The lstrip string method allows us to remove content from the left side of a string. The lstrip string method takes one argument in a string format. This argument will remove any combination of the characters in the string object that is contained in the argument. If there is no argument provided to the lstrip string method, then the method will revert to a default white space.



Python Lstrip String Method Syntax

'String Object'.lstrip('characters to be removed')



'String Object' - This is our string object that we are going to remove content from the left side of the string.

.lstrip('characters to be removed') - The lstring string method has one optional argument. This argument will remove any combination characters in the argument from the left of the string object. The default argument is white space if we do not include an argument then Python will remove any spaces from the left side of the string.

Examples Of The Python Lstrip String Method

#Example 1

 '   This is a string   '.lstrip()

'This is a string   '



#Example 2

 'abcdef'.lstrip('cba')

'def'



#Example 3

 'abcdef'.lstrip('ABC')

'abcdef'



#Example 4

'http://www.learnpython.com'.lstrip('/:.wpth')

'learnpython.com'

Examples Explained



Example 1:



'   This is a string   '.lstrip() - We create a string object that contains spaces on the left and right side of the string object. Then we call the lstrip string method on the string object.



'This is a string   ' - We are returned a new string object with the spaces on the left removed since the default argument for the lstrip string method is spaces.



Example 2:



'abcdef'.lstrip('cba') - We create a new string object and call the lstrip string method on our string object. Our string method takes an argument of 'cba' so if any of these characters show up in our string object they will be removed.



'def' - We are returned 'def' since our arguments met the first three characters of our string object.



Example 3:



'abcdef'.lstrip('ABC') - We create a string object and call lstrip string method on our string object. We provide an argument of 'ABC'.



'abcdef' - We are returned the same string since the argument is case sensitive so it did not match any of the characters in the string object.



Example 4:



'http://www.learnpython.com'.lstrip('/:.wpth') - We create a string object that contains a web address and then we call the lstrip string method to remove the 'http://www.' from the web address.  We only need to add one character to the argument.



'learnpython.com' - We are returned a web address with the 'http://www.' removed.



Conclusion



In this Python tutorial we look at the Python lstrip string method.  This method allows us to remove content from the left side of a string. If you have any questions about the lstrip string method leave a comment below.

No comments:

Post a Comment