Python String rstrip() Method

13-01-23 Ahmed Obaid 935 0

​The rstrip() function returns a copy of the string with the last characters removed from the end of the string (depending on the passed character argument). This means that the rstrip() function removes all the specified characters from the right side of the string. If no argument is passed, it removes blank spaces from the right side.


How to formulate it like this:


string. rstrip(characters)

characters: [Optional] A set of characters to remove. By default, it removes blank spaces from the right side.

Return value: Returns a new string with the characters to be removed or spaces removed from the right side.


In the following example, the rstrip() function will, by default, remove white spaces from the end of the string.

Example:


# Python rstrip() method example
# Create variables
string1 = "python and django "
string2 = string1. rstrip()
# Show result
print("Old string: ",string1)
print("New String: ",string2)

The output will be:


Old string: python and django
New String: python and django

inundefined In the following example, we will select one or more characters to remove from the end of the string, in any order.

 Example:


 string = "learn python with ahmed obaid**z*x..N.+!!+*"
 # Removes given set of characters from right.
 print(string. rstrip("+.!*xNz")) # remove +.!*xNz

 The output will be:


learn python with ahmed obaid

 Example:


 string = "https://ahmedobaid.com/python/"
 # Removes given set of characters from right.
 print(string. rstrip("/python/")) # remove /python/

 The output will be:


https://ahmedobaid.com

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python Python String string methods python python string rstrip method

Share page