Tuesday, October 6, 2015

Python Tutorial: Python Rstrip String Method - Python Strings #70





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



Python Rstrip String Method

Python Rstrip String Method

In this tutorial, we will look at the Python rstrip string method. The rstrip string method strips content from the right side of the of the string. The rstrip string method takes one argument which is referred to as the chars which are the characters we want to remove from the string. The argument must be in a string format, but these characters don't have to be in any specific order. When the rstrip string method crosses a character that is not contained in the argument rstrip will stop processing. The default argument will remove white spaces from the right side of the string object.



Python Rstrip String Method Syntax

'String Object'.rstrip('chars')



'String Object' - Our string object which we will call the rstrip string method on.

.rstrip() - The rstrip string method will remove content from the right side of the string method. The rstrip() will take the characters contained in the argument and remove those characters right to left. When rstrip comes across a character that is not contained in the argument than the rstrip method will stop.

'Chars' - This is the argument that we provide the rstrip string method. This method will search right to left and remove any characters from the string object that is contained in the argument. The argument is case-sensitive. If we leave this argument blank then the string method argument will default to white spaces and the string method will only remove white spaces from the right side of the string object.

Examples Of The Python Rstrip String Method

#Example 1

 '    Python    '.rstrip()

'    Python'



#Example 2

 'We are learning Python'.rstrip('nohtyP')

'We are learning '



#Example 3

 'CAPS'.rstrip('SpAc')

'CAP'

Examples Explained



Example 1:



'    Python    '.rstrip() - In this example, we create a string object that contains spaces on the right and left side. We then call the rstrip string method on our string object. We provide the string method with a blank argument which will default to remove white spaces on the right side of the of the string object.



'    Python' - We are returned a new string object that contains no white spaces on the right of the string object.



Example 2:



'We are learning Python'.rstrip('nohtyP') - In this example, we create a new string object and then call the rstrip string method on the string object. We provide the string method with an argument of ('nohtyP') which is Python spelled backwards. This example shows that it does not have to be in a specific order. Note that we use uppercase 'P' since the rstrip string method is case-sensitive.



'We are learning ' - We are returned a new string object that has 'Python' removed.  Notice we did not remove the whitespace since there was no whitespace in the argument.



Example 3:



'CAPS'.rstrip('SpAc') - In this example we create a new string object and call the rstrip string method on our string object. This example, proves that rstrip is case senstive.



'CAP' - Notice that we only removed the 'S' from the string object. This was done because the argument is case-sensitive.



Conclusion



In this Python tutorial, we took a look at the Python rstrip string method if you have any questions about this string method leave a comment below.

No comments:

Post a Comment