Python string rstrip() method

Python String rstrip() Method

Description

The rstrip() method returns a copy of a string with all characters removed from the end of the string (whitespace characters by default).

Syntax

The syntax of the rstrip() method is as follows:

var.rstrip([chars])

Parameters

chars - You can specify the characters to be removed.

Return Value

This method returns a copy of a string with all characters removed from the end of the string (whitespace characters by default).

Example

The following example demonstrates the use of the rstrip() method.

var = " Hello Python "
var1 = var.rstrip()
print ("Original string:", var)
print ("String after removing right spaces:", var1)

var = "0001234000"
var2 = var.rstrip('0')
print ("Original string:", var)
print ("String after removing right '0':", var2)

var = 'this is string example....wow!!!'
var3 = var.rstrip('!')
print ("Original string:", var)
print ("String after removing the '!' on the right:", var3)

When you run this program, it will produce the following output

Original string: Hello Python
String after removing spaces on the right: Hello Python
Original string: 0001234000
String after removing the '0' on the right: 0001234
Original string: this is string example....wow!!!
String after removing the '!' on the right: this is string example....wow

Leave a Reply

Your email address will not be published. Required fields are marked *