Introduction to Python rstrip()
In Python as there is no data type declaration 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 as a string in Python. In Python, there are many string methods 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.
To do this, Python has a built-in function known as strip(). This method returns the part of the string that has been stripped from the original string by removing leading and trailing characters. In this, we are focusing 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 leading and trailing characters of the given string.
Syntax:
Str.strip()
Parameters:
- You can have char as the parameter, which is optional which is used to notify how many characters are to be removed from the original string.
- If no parameters are specified, then it takes by default it removes the whitespaces from the right and left side 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 for 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 ” will result in by removing the whitespace at starting of the string and end of the given string and gives “abc Educba training abc”. When we use 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’) then it removes the character ‘abc’ at the starting and end of the string and gives the string as “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 a parameter that is optional; 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 that is stripped from the end of the given string. The characters specified in the function are striped from the right side of the given string. By default, the rstrip() function strips out 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, 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 it 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 the project reports and documents, can also be used when you want to publish a book and you want to strip out extra whitespaces or newlines; these are helpful in such real-world examples. The strip() and rstrip() functions can also be used in removing 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 very important methods like strip(), rstrip() 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 specified, then it strips out by default whitespaces. The rstrip() and lstrip() functions also work similar to strip() function only difference is rstrip() strips out characters from right side and lstrip() strips out characters from left side. And by default, for both functions, it strips out whitespaces if no parameters specified.
Recommended Articles
This is a guide to Python rstrip(). Here we discuss the syntax and working of Python rstrip() along with examples and code implementation. You can also go through our other suggested articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses