Python

Python rstrip() function

Python has some built-in functions to remove space from the left-side, right-side, and both sides of the string value. The rstrip() function is a built-in python function to remove the space from the right side of the string value. The uses of this function have shown in this tutorial.

Syntax:

The syntax of this function has given below.

string.rstrip([chars])

The argument of this function is optional that can take one or more character values based on which the content of the string will be removed from the right side. If no argument value is provided in the function, then extra space will be removed from the right side. The function returns the modified string after removing the space or the particular characters.

Different uses of rstrip() function:

Example-1: Use of rstip() function without argument

Create a python file with the following script where the rstrip() function has been used without any argument to remove space from the right side of the string. Two string variables have been declared in the script, and the variable named string1 contains extra space at the end of the value. These two strings have been combined and printed before and after using the rstrip() function.

# Declare a string by adding space in right-side

string1 = "Linuxhint. "

# Declare another string

string2 = "Python."

# Combine the string

output = string1 + string2

print("Before using rstrip() function:\n", output)

# Combine the string after using rstrip()

output = string1.rstrip() + string2

print("After using rstrip() function:\n", output)

Output:

The following output will appear after executing the above script. According to the output, the extra space has been removed from the string1 variable after using the rstrip() function.

Example-2: Use of rstip() function with argument

Create a python file with the following script where the rstrip() function has been used with a single character and multiple characters to remove the particular one or more characters that match with the last part of the main string value. Two string variables have been declared in the script. One contains a single character, and another contains four characters. The values of these variables have been used in the rstrip() function for stripping data from the main string from the right side.

# Declare a string

stringVal = "Welcome to Linuxhint"

# Define the character to remove from the right-side

stripVal = 't'

# Print the string after removing the character

print("Remove single character from the right-side:\n", stringVal.rstrip(stripVal))

# Define the characters to remove from the right-side

stripVal = 'hint'

# Print the string after removing the characters

print("Remove multiple character from the right-side:\n", stringVal.rstrip(stripVal))

Output:

The following output will appear after executing the above script. According to the output, the first rstrip() function has removed the character, ‘t‘, and the second rstrip() function has removed the string, ‘hint’ from the last part of the string, ‘Welcome to Linuxhint’.

Example-3: Use of rstrip() function with the argument to check case-sensitivity

Create a python file with the following script where the rstrip() function has been used with the argument two times. Two variables that are used for stripping contain the same characters with one difference. One contains the small letter, ‘m’, and another contains the capital letter, ‘M’. The main string contains the small letter ‘m’ two times at the end of the string.

# Declare a string

stringVal = "Python programming"

# Define the characters to remove

stripVal = 'gnim'

# Print the string after removing the characters

print("The output after removing the characters:\n", stringVal.rstrip(stripVal))

# Define the characters to remove

stripVal = 'gniM'

# Print the string after removing the characters

print("The output after removing the characters:\n", stringVal.rstrip(stripVal))

Output:

The following output will appear after executing the above script. According to the output, the first rstrip() function has removed the characters, ‘mming’ for the argument value, ‘gnim’ and the second rstrip() function has removed the string, ‘ing’ for the argument value, ‘gniM’ from the end of the string, ‘Python Programming’.

Conclusion:

The uses of the rstrip() function with argument and without arguments have been shown in this tutorial by using simple python examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.