How Can We Help?
In this tutorial, we will explain how to use Python string rstrip() method with basic syntax by example for better understanding.
The String rstrip() method or String Right Strip Method removes particular characters or space of a string from right direction.
Syntax:
<string_value/string_variable>.rstrip(strip character)
OR
<string_value/string_variable>.rstrip()
Input Parameters:
- string_value / String variable : The string that we want to remove a particular character from the right hand side.
- Strip Character : The character you want to eliminate from the string.
String rstrip Special Character
We Can Strip or remove special characters from the right-hand side of a string by giving a special character argument to the rstrip function. ex: string.rstrip(‘$’).
Example (1)
# The String Value That We Want To Perform Strip Against
str1='$$$python36$$'
#Print String After Strip $ Character from it
print('Right Strip $: ',str1.rstrip('$'))
Output of example (1)
Right Strip $: $$$python36
In the above example, ‘$’ is existent at the start and the end in the string value of string variable ‘str1’, and we have removed only those special characters from the right-hand side of a string by using str1.rstrip(‘$’) in the print statement.
String rstrip Whitespace
We Can Strip or remove the white spaces from the right-hand side of a string by calling the rstrip function without passing any argument. ex: string.rstrip().
Example (2)
# The String Value That We Want To Perform Strip Against
str2=' $$$python36$$ '
#Print String After removing whitespace from it
print('Right Strip:',str2.rstrip())
Output of example (2)
Right Strip $: $$$python36
In the above example, white spaces are existent at the start and the end in the string value of string variable ‘str1’, and we have removed only those white spaces from the right-hand side of a string by using string.rstrip() in the print statement.
String rstrip Newlines
We Can Strip or remove newlines from the right-hand side of a string by giving a new line symbol an argument to the right strip function. ex: string.rstrip(‘\n’).
Example (3)
# The String Value That Contains newlines before and after the string
str3='\n$$$python36$$\n'
#Print String After removing newlines from it
print('str3 value after removing new line:',str3.strip('\n'))
Output of example (3)
Strip: $$$python36$$
In the above example, Newlines are existent before and after the string value of string variable ‘str1’, and we have removed the new line that occurs on the right-hand side of a string by specifying the argument ‘\n’ , which is referring to a new line to right strip function in the print statement.
String rstrip Quotes
We Can Strip or remove quotes from only the right-hand side of a string by giving a quotation mark as an argument to the right strip function. ex: string.rstrip(‘”‘).
Example (4)
# The String Value That Contains quotes before and after the string value
str4='"Learn Python With Oraask"'
#Print String After removing quotes on the right side
print('str4 value after removing quotes:',str4.rstrip('"'))
Output of example (4)
str4 value after removing quotes: “Learn Python With Oraask
In the above example, quotation marks are existent before and after the string value of string variable ‘str1’. We have removed them from the right-hand side of a string by specifying the argument ‘“‘ to strip function in the print statement.
In this tutorial, you have learned the string rstrip() method in Python. What is it? And how to remove a special characters, whitespaces, quotes and newlines from only the right hand side of a string.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Formatting Methods:
- title() : It returns the “title cased” version of string means all the words start with uppercase and the remaining are lowercase.
- capitalize() : It capitalizes the first character of the String.
- center() : It returns a space-padded string with the original string centered to a total of width columns.
- swapcase() : It inverts the case of all the characters in a string.
- upper() : It converts all the characters of a string to uppercase.
- lower() : It converts all the characters of a string to lowercase.
- strip() : It removes particular characters of a string from left and right side.
- lstrip() : It removes particular character of a string from left side.
- ljust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- rjust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- zfill() : It returns an original string value with filled zero at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.