Reference Materials
Created with over a decade of experience.
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python String Methods
- Python String capitalize()
- Python String center()
- Python String casefold()
- Python String count()
- Python String endswith()
- Python String expandtabs()
- Python String encode()
- Python String find()
- Python String format()
- Python String index()
- Python String isalnum()
- Python String isalpha()
- Python String isdecimal()
- Python String isdigit()
- Python String isidentifier()
- Python String islower()
- Python String isnumeric()
- Python String isprintable()
- Python String isspace()
- Python String istitle()
- Python String isupper()
- Python String join()
- Python String ljust()
- Python String rjust()
- Python String lower()
- Python String upper()
- Python String swapcase()
- Python String lstrip()
- Python String rstrip()
- Python String strip()
- Python String partition()
- Python String maketrans()
- Python String rpartition()
- Python String translate()
- Python String replace()
- Python String rfind()
- Python String rindex()
- Python String split()
- Python String rsplit()
- Python String splitlines()
- Python String startswith()
- Python String title()
- Python String zfill()
- Python String format_map()
Python String rstrip()
The rstrip() method returns a copy of the string with trailing characters removed (based on the string argument passed).
Example
title = 'Python Programming '
# remove trailing whitespace from title
result = title.rstrip()
print(result)
# Output: Python Programming
Syntax of String rstrip()
The syntax of rstrip() is:
string.rstrip([chars])
rstrip() Parameters
- chars (optional) - a string specifying the set of trailing characters to be removed.
The rstrip() removes characters from the right based on the argument (a string specifying the set of characters to be removed).
If chars argument is not provided, all whitespaces on the right are removed from the string.
Return Value from rstrip()
The rstrip() returns a copy of the string with trailing characters stripped.
All combinations of characters in chars argument are removed from the right of the string until the first mismatch.
Example: Working of rstrip()
random_string = 'this is good '
# Trailing whitespace are removed
print(random_string.rstrip())
# 'si oo' are not trailing characters so nothing is removed
print(random_string.rstrip('si oo'))
# in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
print(random_string.rstrip('sid oo')) website = 'www.programiz.com/'
print(website.rstrip('m/.'))
Output
this is good this is good this is g www.programiz.co
Also Read:
Did you find this article helpful?
