Python string Method - rstrip()

www.ig‮fi‬tidea.com

The rstrip() method is a built-in string method in Python that returns a copy of a string with trailing whitespace characters removed from the right side.

The syntax for using rstrip() method is as follows:

string.rstrip(characters)

Here, string is the original string, and characters is an optional parameter that specifies the set of characters to remove from the right side of the string. If characters is not specified, the method removes all whitespace characters.

The method returns a new string with trailing characters removed from the right side.

Here's an example of using the rstrip() method:

string = "   Hello, World!   "
trimmed_string = string.rstrip()
print(trimmed_string)

Output:

Hello, World!

In the example above, the rstrip() method was used to remove the trailing whitespace characters from the original string " Hello, World! ". The resulting string is stored in the variable trimmed_string. Note that the leading whitespace characters are still present in the string. To remove both leading and trailing whitespace characters, you can use the strip() method.