Easy Tutorial
❮ Ref Set Difference_Update Python Mongodb Insert Document ❯

Python3 rstrip() Method

Python3 Strings


Description

The rstrip() method removes specified characters from the end of a string. By default, it removes trailing whitespace characters, including spaces, newlines, carriage returns, and tabs.

Syntax

The syntax for the rstrip() method is:

str.rstrip([chars])

Parameters

Return Value

Returns a new string with the specified characters removed from the end of the original string.

Examples

The following examples demonstrate the usage of the rstrip() function:

#!/usr/bin/python3

random_string = 'this is good    '

# Trailing spaces will be removed
print(random_string.rstrip())

# 'si oo' are not trailing characters, so nothing is removed
print(random_string.rstrip('si oo'))

# 'd oo' are trailing characters in 'sid oo', 'ood' is removed from the string
print(random_string.rstrip('sid oo'))

# 'm/' are trailing characters, no trailing '.' is found, 'm/' is removed from the string
website = 'www.tutorialpro.org/'
print(website.rstrip('m/.'))

# Removes trailing commas (,), dots (.), letters s, q, or w
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)

# Removes trailing asterisks (*)
str = "*****this is string example....wow!!!*****"
print(str.rstrip('*'))

The output of the above examples is as follows:

this is good
this is good
this is g
www.tutorialpro.co
banana
*****this is string example....wow!!!

Python3 Strings

❮ Ref Set Difference_Update Python Mongodb Insert Document ❯