How to Use String rstrip() Method in PythonPython Basics


The Rstrip() method removes spaces or the specified characters at the end of a string.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

str = "have a nice day!!!"
print (str)
print ('After right-strip: ', str.rstrip('!'))

str = "have a nice dayday"
print (str)
print ('After right-strip: ', str.rstrip('day'))

Output:

have a nice day!!!
After right-strip:  have a nice day
have a nice dayday
After right-strip:  have a nice

Syntax

str.rstrip([chars])

Parameters

Name Description
chars Specifies the characters to be deleted (default is a space)

Return Value

It returns a copy of the string with trailing characters stripped.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us