Scripting

Python rstrip() Function

Python rstrip() Function

Introduction

The rstrip() function in Python allows you to remove the characters you specify at the end of a string. If no characters are specified, it will remove blank spaces. Whitespace is the default character that the function will remove.

This is a useful function to remove unwanted characters quickly. Now we will show you about using the rstrip() function in Python.

Example

txt = " cat "

x = txt.rstrip()

print(x)

Output:

cat

Definition

The rstrip() function removes any character you specify. It only removes the characters at the end.

Whitespace is the default character that the function will remove.

The syntax

string.rstrip(chars)

Parameter Values:

chars: the characters you request to remove

More examples

Example 1:

str = "pythonaaaa"

x = str.rstrip('a')

print(x)

Output:

python

Example 2:

str = "cateri,."

x = str.rstrip("eir,.")

print(x)

Output:

cat

Conclusion

We just showed you to use the rstrip() function in Python.

Thank you for reading!

 

Similar Posts