Tuesday, October 6, 2015

Python Tutorial: Python Strip String Method - Python String #72



For more information visit our website at http://learnpythontutorial.com/python-strip-string-method/



Python Strip String Method

Python Strip String Method

In this Python tutorial, we're going to focus on the Python strip string method. Python strip string method has the capability to strip content from the left or right side of the string object. Strip string method takes one argument the argument must be in a string format and if this argument is blank then it will move the white space from either left or right or both sides of the string object. The strip string method removes content from the left and right hand side of the string object where the left strip string method removes content from the left-hand side and the right strip string method removes from the right-hand side of a string object.



Python Strip String Method Syntax

'String Object'.strip('chars')



'String Object' - This is our string object which were going to call the python strip string method on.

.strip() -  The strip string method removes content from the left and right side of the string object. When the strip string method comes across the character that does not match the argument the strip string method will stop.

'Chars' - This argument is case-sensitive and does not have to be in any particular order. Python will take the argument and remove any characters that matched the string object and remove those characters and return a new string object.

Example Of The Python Strip String Method

#Example 1

 '    This is a string    '.strip()

'This is a string'



#Example 2

 'This is a string in Python'.strip('nThiosn')

' is a string in Pyt'



#Example 3

 'http://learnpythontutorial.com'.strip('htp://')

'learnpythontutorial.com'

Examples Explained



Example 1:



'    This is a string    '.strip() - In this example we create a string object and we called the strip string method on our string object. We provide no argument so the strip string method will remove only white space.



'This is a string' - We are return a string object says no whitespace on either the left or right inside.



Example 2:



'This is a string in Python'.strip('nThiosn') - In this example we create a new string object and we call the strip string method on our string object. We provide an argument that contains some characters that would like to move from the left and right hand side of the string object.



' is a string in Pyt' - We are returned a string object with some characters made from the left and right side of the string object.



Example 3:



'http://learnpythontutorial.com'.strip('htp://') - in this example we create string object and we give an argument to remove HTTP:// from our string object.



'learnpythontutorial.com' - We are returned a new string object that removes the 'http://' from the string object.



Conclusion



In this Python tutorial we look at the Python strip string method. If you have any questions please leave a comment below.

No comments:

Post a Comment