In the previous article, we have discussed Python String rsplit() Method Examples
rstrip() Method in Python:
The rstrip() method removes any trailing characters (characters at the end of a string); the default trailing character to remove is space.
Syntax:
string.rstrip(characters)
Parameters
characters: This is optional. A set of characters that should be removed as trailing characters.
Examples:
Example1:
Input:
Given string = " pythonprograms,,,,,,," Given characters = ","
Output:
The above given string is : pythonprograms,,,,,,, The given string after applying rstrip() function: pythonprograms
Example2:
Input:
Given string = "python-programs_!!!!!&&&&" Given characters = "!&"
Output:
The above given string is : python-programs_!!!!!&&&& The given string after applying rstrip() function: python-programs_
String rstrip() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Give the character as static input and store it in another variable.
- Apply rstrip() method to the given string for the given character to remove any trailing characters (characters at the end of a string); the default trailing character to remove is space.
- Store it in another variable.
- Print the above-given string
- Print the above-given string after applying rstrip() function.
- Similarly, do the same for other the string without giving the character and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = " pythonprograms,,,,,,," # Give the character as static input and store it in another variable. gvn_chrctr = "," # Apply rstrip() method to the given string for the given character to remove # any trailing characters (characters at the end of a string); the default # trailing character to remove is space. # Store it in another variable. rslt_str1 = gvn_fststr.rstrip(gvn_chrctr) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying rstrip() function. print("The given first string after applying rstrip() function:", rslt_str1) # Similarly do the same for other string without giving the character # and print the result string. gvn_scndstr = "?????good morning " rslt_str2 = gvn_scndstr.rstrip() print("The above given second string is :", gvn_scndstr) print("The given second string after applying rstrip() function:", rslt_str2)
Output:
The above given first string is : pythonprograms,,,,,,, The given first string after applying rstrip() function: pythonprograms The above given second string is : ?????good morning The given second string after applying rstrip() function: ?????good morning
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Give the character as user input using the input() function and store it in another variable.
- Apply rstrip() method to the given string for the given character to remove any trailing characters (characters at the end of a string); the default trailing character to remove is space.
- Store it in another variable.
- Print the above-given string
- Print the above-given string after applying rstrip() function.
- Similarly, do the same for the other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some random string = ") # Give the character as user input using the input() function and store it in another variable. gvn_chrctr = input("Enter some random character = ") # Apply rstrip() method to the given string for the given character to remove # any trailing characters (characters at the end of a string); the default # trailing character to remove is space. # Store it in another variable. rslt_str1 = gvn_fststr.rstrip(gvn_chrctr) # Print the above given string print("The above given first string is :", gvn_fststr) # Print the above given string after applying rstrip() function. print("The given first string after applying rstrip() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = input("Enter some random string = ") gvn_chrctr2 = input("Enter some random characters = ") rslt_str2 = gvn_scndstr.rstrip(gvn_chrctr2) print("The above given second string is :", gvn_scndstr) print("The given second string after applying rstrip() function:", rslt_str2)
Output:
Enter some random string = ###btechgeeks****@@@@ Enter some random character = * The above given first string is : ###btechgeeks****@@@@ The given first string after applying rstrip() function: ###btechgeeks****@@@@ Enter some random string = python-programs_!!!!!&&&& Enter some random characters = !& The above given second string is : python-programs_!!!!!&&&& The given second string after applying rstrip() function: python-programs_
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.