rstrip(): Remove Trailing Characters

The rstrip() method removes trailing whitespace or specified characters from the end of a string, often used in data cleaning tasks to ensure consistency in string formatting.

Syntax

string.rstrip([chars])
-chars (optional): Specifies characters to remove from the end. Defaults to whitespace if omitted.
my_str='Plus2net    '
output=my_str.rstrip()
print(output)

my_str='plus2net**'
output=my_str.rstrip('*')
print(output) # removes * from right

my_str='plus2net *#*#'
output=my_str.rstrip('*#')
print(output) # removes all right 
Output is here
plus2net
plus2net
plus2net
Example
my_str='Plus2net'
output=my_str.rstrip('et')
print(output)
Output
Plus2n

Example : Edge Case – Empty String

my_str = ""
output = my_str.rstrip()
print(output)  # Output: ''

Example: Removing Trailing Whitespace

text = "Hello, World!   "
result = text.rstrip()
print(result)
Output:
Hello, World!

Example : Removing Specific Characters

Removing specific characters using `rstrip()`.
text = "plus2net***"
result = text.rstrip("*")
print(result)
Output
plus2net

Example : Removing Multiple Characters

Remove multiple specified characters from the string’s end.
text = "plus2net##@@"
result = text.rstrip("#@")
print(result)
Output
plus2net

Example : rstrip() with Conditional Statements

Remove unwanted characters based on conditions.
text = "data.csv,,,,"
if text.endswith(","):
    text = text.rstrip(",")
print(text)
Output
data.csv

Applications of rstrip()

  • Data Cleaning: Eliminate unwanted characters from text endings.
  • File Processing: Strip unnecessary punctuation or spaces in file data.
  • Text Formatting: Standardize string endings for consistent formatting.


All String methods


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer