Updated June 27, 2023
Introduction to Python rstrip()
There is no data type declaration in Python as in other programming languages. Therefore Python does not have any data type for a character, and a sequence of characters or array of characters forms a string, and a single character is also considered a string in Python. In Python, many string methods are introduced, which are built-in functions. So in this article, we are discussing the string method for removing part of the string of characters from the beginning and end of the given string.
Python has a built-in function known as strip() to do this. This method returns the part of the string stripped from the original string by removing leading and trailing characters. In this, we focus only on strip(), which strips only the characters from the right side of the original string to obtain the stripped string.
Working of the strip() and rstrip() in Python with syntax and example
Python has a string method to strip out the characters from the given string to obtain stripped string. This is a built-in function that again has two strip() functions they are rstrip() and lstrip(). The strip() method removes the set of characters from the leading and trailing string characters.
Syntax:
Str.strip()
Parameters:
- You can have char as the optional parameter, which is used to notify how many characters will be removed from the original string.
- If no parameters are specified, then by default, it removes the whitespaces from the right and left sides of the given string.
The return value for the strip() function always returns the value as a string that includes all characters or the stripped string from the given string’s starting and end. It will strip out whitespaces from the given string with leading and trailing whitespaces in the string by default.
Example of Python rstrip()
This code gives an example of the strip() method:
Code:
str = " abc Educba training abc "
str_out = str.strip()
print("Strips whitespaces only", str_out)
str_out1 = str.strip(' abc ')
print("Strips characters specified in the method",str_out1)
Output:
The above example uses strip() without any parameter, so the string is given “abc Educba training ABC,” which will result in removing the whitespace at starting of the string and end of the given string and gives “abc Educba training abc.” When we use the strip() function with parameters, then in this above example, we have specified characters as “abc” as the parameter in the strip()method as strip(‘abc’). It removes the character ‘abc’ at the start and end of the string and gives the string “Educba training” as output.
How to use rstrip() in Python?
This is also a string method that is part of the strip() method, rstrip() function only removes the string from the right side that is from the end of the original string or removes the trailing characters of the original string.
Syntax:
Str.rstrip()
Parameters:
- This is again similar to the strip()method, which has char as an optional parameter; it is only used when specified characters are to be removed.
- If no parameters are specified, then by default, it will strip whitespace from the right side of the given string.
The return value for the rstrip() function is always a string. This returns the stripped string from the end of the given string. The function strips the specified characters from the right side of the given string. By default, the rstrip() function strips the trailing whitespaces from the string.
Example #1
Let us consider an example that uses the rstrip() method in the program to strip out the right-hand side of the given string.
Code:
str = " abc Educba training abc "
str_out = str.rstrip()
print("Strips whitespaces only", str_out)
str_out1 = str.rstrip(' abc ')
print("Strips characters specified in the method",str_out1)
Output:
In the above example, we have used the rstrip() method with and without parameters. In the above example, the rstrip() function without parameter when applied on the given string as “abc Educba training abc” then this method strips out the whitespace from the right-hand side of the given string and results in the string as “abc Educba training abc.” The rstip() function with parameter as ‘abc’ when applied on the given string as “abc Educba training abc” then results in the string as “abc Educba training,” which again strips out characters specified inside the parameters as ‘abc’ from the right side of the string.
These string strip() and strip() functions, which can be used in formatting project reports and documents, can also be used when you want to publish a book and strip out extra whitespaces or newlines; these are helpful in such real-world examples. The strip() and rstrip() functions can also remove the newline from the given string. First, let us see how strip() can remove the newline from the given string.
Example #2
To remove the newline using the strip() function from the given string.
Code:
str = "\n....Educba training.....\n"
print(str)
str_out = str.strip('\n')
print "\"" + str_out + "\""
Output:
Example #3
To remove the newline from the right side of the string:
Code:
str = "Educba training.....\n"
print(str)
str_out = str.strip('\n')
print "\"" + str_out + "\""
Output:
Conclusion
In Python, the string has essential methods like strip(), strip(), and lstrip(). The strip() function usually strips out the characters that are at starting and end of the string, which are specified in the parameter of the function, and if no parameters are specified, then it strips out by default whitespaces. The rstrip() and lstrip() functions also work similarly to strip(); the function’s only difference is rstrip() strips out characters from the right side, and lstrip() strips out characters from the left side. And by default, it strips out whitespaces for both functions if no parameters are specified.
Recommended Articles
This is a guide to Python rstrip(). Here we discuss the syntax and working of Python rstrip(), examples, and code implementation. You can also go through our other suggested articles to learn more –