SlideShare a Scribd company logo
1 of 4
Download to read offline
Developer Helps
PYTHON
Python String rstrip() Method
In this tutorial, we will learn about rstrip() Method in Python. The rstrip() method in Python is like a tool
to tidy up strings. It takes away extra spaces, tabs, and newlines from the right side of a string. For
instance, if you have a sentence with spaces at the end like. ” This is an example string with trailing
spaces. “, you can use rstrip() to get rid of those spaces.
This method helps you handle and clean up the text. When you have data with unnecessary spaces or
tabs towards the end.
RELATED POST: Python String lower() method
Rstrip() method Syntax
New_string = original_string.rstrip([characters])
Parameters Details:
Original_string: This is the string from which you want to remove trailing whitespace.
Characters (optional): A string containing the specific characters you want to remove from the right side
of the original string. If not provided, the method will remove all whitespace characters.
Python rstrip() method return value
The rstrip() method in Python returns a new string with trailing (right-side) whitespace characters
removed. It does not modify the original string; instead, it creates and returns a modified copy of the
string.
Check out My Latest post on Developer Helps for some Interesting Insights
↠ What is Self in Python?
↠ What do you mean by Python Enum?
↠ How to Convert Kilometers to Miles in Python?
↠ How to Convert Celsius to Fahrenheit in Python?
↠ Python Program to check if a number is odd or even
↠ How to Comment Out Multiple Lines in Python?
Python String rstrip() method Example:
Below are the different examples of the Python String rstrip() method.
Removing trailing spaces from a string by using rstrip() method.
Removing specific characters from the right side of a string by using rstrip() String method.
Combining rstrip() with reassignment to modify the original string.
Using rstrip() to remove trailing tabs from a string.
Chaining multiple String methods including rstrip().
By using rstrip() with a string containing various whitespace characters.
Removing trailing spaces from a Python String
Text = “ Hello, World! “
New_text = text.rstrip()
Print(new_text)
Output:
“ Hello, World!”
In this example, the rstrip() method removes the trailing spaces from the text string. The result new_text
is a new string with the modified content, leaving the original text unchanged.
Removing specific characters from the right side of a String
Text = “ABC123$$$”
New_text = text.rstrip(“$”)
Print(new_text)
Output:
“ABC123”
Combining rstrip() with reassignment to modify the original string
Text = “ Trim me! “
Text = text.rstrip()
Print(text)
Output:
“ Trim me!”
By using rstrip() to remove trailing tabs from a Python String
Code = “tif x > 10:ttt”
Clean_code = code.rstrip(“t”)
Print(clean_code)
Output:
“tif x > 10:”
Only the trailing tabs were removed because the “t” argument was passed to rstrip().
Chaining multiple String methods including rstrip()
Data = “ 42 apples n”
Cleaned_data = data.strip().upper().rstrip(“S”)
Print(cleaned_data)
Output:
“42 APPLE”
By using rstrip() with a string containing various whitespace characters
Content = “This is a sentence.nt “
Trimmed_content = content.rstrip(“nt “)
Print(trimmed_content)
Output:
“This is a sentence.”
The rstrip() method returns a new string with the specified modifications while leaving the original string
unchanged. If you want to update the original string, you need to reassign the result back to the original
variable.

More Related Content

Similar to Python String rstrip() Method.pdf

regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdfDarellMuchoko
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptxPadreBhoj
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings FormatMr Examples
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and dataSukhpreetSingh519414
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & ImportMohd Sajjad
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptxssuser8e50d8
 
Java scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerJava scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerEdward Nyang'ali
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String FunctionsAvanitrambadiya
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212Mahmoud Samir Fayed
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdfGaneshRaghu4
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaEdureka!
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functionsmatlab Content
 

Similar to Python String rstrip() Method.pdf (20)

regular-expression.pdf
regular-expression.pdfregular-expression.pdf
regular-expression.pdf
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
unit-4 regular expression.pptx
unit-4 regular expression.pptxunit-4 regular expression.pptx
unit-4 regular expression.pptx
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Python Strings Format
Python Strings FormatPython Strings Format
Python Strings Format
 
ppt notes python language operators and data
ppt notes python language operators and datappt notes python language operators and data
ppt notes python language operators and data
 
ACP-arrays.pptx
ACP-arrays.pptxACP-arrays.pptx
ACP-arrays.pptx
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Java scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java ScannerJava scanner, everything you need to know about Java Scanner
Java scanner, everything you need to know about Java Scanner
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Java
JavaJava
Java
 
The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212The Ring programming language version 1.10 book - Part 45 of 212
The Ring programming language version 1.10 book - Part 45 of 212
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functions
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Python String rstrip() Method.pdf

  • 1. Developer Helps PYTHON Python String rstrip() Method In this tutorial, we will learn about rstrip() Method in Python. The rstrip() method in Python is like a tool to tidy up strings. It takes away extra spaces, tabs, and newlines from the right side of a string. For instance, if you have a sentence with spaces at the end like. ” This is an example string with trailing spaces. “, you can use rstrip() to get rid of those spaces. This method helps you handle and clean up the text. When you have data with unnecessary spaces or tabs towards the end. RELATED POST: Python String lower() method Rstrip() method Syntax New_string = original_string.rstrip([characters]) Parameters Details: Original_string: This is the string from which you want to remove trailing whitespace. Characters (optional): A string containing the specific characters you want to remove from the right side of the original string. If not provided, the method will remove all whitespace characters. Python rstrip() method return value The rstrip() method in Python returns a new string with trailing (right-side) whitespace characters removed. It does not modify the original string; instead, it creates and returns a modified copy of the string. Check out My Latest post on Developer Helps for some Interesting Insights ↠ What is Self in Python? ↠ What do you mean by Python Enum?
  • 2. ↠ How to Convert Kilometers to Miles in Python? ↠ How to Convert Celsius to Fahrenheit in Python? ↠ Python Program to check if a number is odd or even ↠ How to Comment Out Multiple Lines in Python? Python String rstrip() method Example: Below are the different examples of the Python String rstrip() method. Removing trailing spaces from a string by using rstrip() method. Removing specific characters from the right side of a string by using rstrip() String method. Combining rstrip() with reassignment to modify the original string. Using rstrip() to remove trailing tabs from a string. Chaining multiple String methods including rstrip(). By using rstrip() with a string containing various whitespace characters. Removing trailing spaces from a Python String Text = “ Hello, World! “ New_text = text.rstrip() Print(new_text) Output: “ Hello, World!” In this example, the rstrip() method removes the trailing spaces from the text string. The result new_text is a new string with the modified content, leaving the original text unchanged.
  • 3. Removing specific characters from the right side of a String Text = “ABC123$$$” New_text = text.rstrip(“$”) Print(new_text) Output: “ABC123” Combining rstrip() with reassignment to modify the original string Text = “ Trim me! “ Text = text.rstrip() Print(text) Output: “ Trim me!” By using rstrip() to remove trailing tabs from a Python String Code = “tif x > 10:ttt” Clean_code = code.rstrip(“t”) Print(clean_code) Output: “tif x > 10:” Only the trailing tabs were removed because the “t” argument was passed to rstrip(). Chaining multiple String methods including rstrip() Data = “ 42 apples n” Cleaned_data = data.strip().upper().rstrip(“S”)
  • 4. Print(cleaned_data) Output: “42 APPLE” By using rstrip() with a string containing various whitespace characters Content = “This is a sentence.nt “ Trimmed_content = content.rstrip(“nt “) Print(trimmed_content) Output: “This is a sentence.” The rstrip() method returns a new string with the specified modifications while leaving the original string unchanged. If you want to update the original string, you need to reassign the result back to the original variable.