Python String rstrip() is an inbuilt method that returns a copy of the String with trailing characters removed. The rstrip() function returns the copy of the String in which all characters have been stripped from the end of the string and default whitespace characters.
One most important point to note here that these functions do not make inplace changes, and thus, these changes are just temporary.
If you want to make these modifications permanent then, you need to assign it to another variable to store the changed String because it returns the changed String.
Another important thing is by default, and it removes whitespaces if you don’t mention the character.
string.rstrip([chars])
rstrip()
removes characters from the right based on the argument (a string specifying the set of characters to be removed).rstrip()
returns a copy of the string with trailing characters stripped.rand_str = "Harry Potter "
result = rand_str.rstrip()
print(result);
Output
Harry Potter
rand_str = "Harry Potter"
result = rand_str.rstrip("tter")
print(result);
Output
Harry Po
string = "homer simpson"
print(string.rstrip('sim'))
Output
homer simpson
Thanks for reading !!!
#python