Advertisement



< Prev
Next >



Python - String rstrip() Method




The rstrip() string method is used to strips all the whitespace characters present at the right/end of the string i.e. trailing whitespaces in a string.

Note : None of the string methods change the original string and only return a new string which reflects the change performed by the string method, hence the returned string must be stored or used(if needed to reflect the result), otherwise it is lost.



Signature of rstrip()


The rstrip() method has no parameters, so we don't have to worry about passing it some value.

# Signature of rstrip() method 
rstrip()




Method rstrip() example


Here in program below, we have a string object that contains a string value with many whitespaces at the end(right side) and we are going to first trim all these trailing whitespaces it by calling rstrip() method on it and next, we will concatenate the trimmed string's value with another string, to create a new string.

# Python -  Program to strip the trailing blank spaces from a string using rstrip() method




# Creating a first string with trailing whitespaces
s1 = 'hello    '


# Creating a string with no trailing whitespaces
s2 = ' there'



# Printing the first string
print('The value of first string : ',  s1)


# Printing the second string
print('The value of second string : ',  s2)



# Calling  the rstrip() method on string object 
print('The trimmed value of first string : ', s1.rstrip())




# Creating a third string by concatenating trimmed form of string with another string, using + operator
s3 = s1.rstrip() + s2


# Printing the third string
print('The value of third  string : ', s3)


Output is :


The value of first string :  hello    
The value of second string :   there
The trimmed value of first string :  hello
The value of third  string :  hello there



Advertisement




Method rstrip() does not modify the original content in a String object


The method rstrip() only returns the content of string object, minus the trailing whitespaces in it. Method rstrip() does not modify the original content of a string object. Let's understand this by an example.


Let us see the same example to prove you our point.
# Python - Method rstrip() does not modify the invoked string



# Creating a first string with trailing whitespaces
s1 = 'humpty         '


# Creating a string with no trailing whitespaces
s2 = 'dumpty'



# Printing the first string
print('The value of first string : ',  s1)


# Printing the second string
print('The value of second string : ',  s2)



# Calling  the rstrip() method on string object 
print('The trimmed value of first string : ', s1.rstrip())



# Creating a third string by concatenating the first string with second, using + operator
s3 = s1 + s2


# Printing the third string
print('The value of third  string : ', s3)


Output is :


The value of first string :  humpty    
The value of second string :   dumpty
The trimmed value of first string :  humpty
The value of third  string :  humpty     dumpty

As you can see in the output, that the first string object contains the same original value even after the method rstrip() was called on it, because the method rstrip() only returns the content of string object, minus the trailing whitespaces in it. It does not modify the original content of a string object.




Strings are immutable but reference variables are mutable.


String objects are immutable, but the reference variables pointing to them are not, therefore in order to display the result of calling the rstrip() method on the original object, we can make the reference variable to the original string, point to the trimmed string value returned by the rstrip() method(but in this case, the original string will be lost).

#Python - Another example of rstrip() method


# Creating the first string
s1 = 'A good smile     '


# Creating the second string 
s2 = ' changes everything'



# Printing the first string
print('The value of first string : ',  s1)


# Printing the second string
print('The value of second string : ',  s2)



# Result of calling rstrip() is made to reference by the reference of original string.
s1 = s1.rstrip()



# Creating a third string by concatenating the first string with second, using + operator
s3 = s1 + s2


# Printing the third string
print('Third string by contenating first and second string : ', s3)


Output is :


The value of first string :  A good smile     
The value of second string :   changes everything
Third string by contenating first and second string :  A good smile changes everything





Please share this article -





< Prev
Next >
< String lstrip() Method
String isnumeric() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement