w3resource

NumPy String: numpy.char.rstrip() function

numpy.char.rstrip() function

For each element in a given array numpy.char.rstrip() function returns a copy with the trailing characters removed. This method is useful for cleaning up strings before processing them further.

Syntax:

numpy.char.rstrip(a, chars=None)

Parameters:

Name Description Required /
Optional
a array-like of str or unicode Required
chars: str or unicode The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped. Optional

Return value:

Output array of str or unicode, depending on input type

Example: Removing trailing whitespace from a string using numpy.char.rstrip()

import numpy as np 
str1 = 'Python NumPy   '
print("Original string: ",str1)
print("After trailing whitepsace are removed:")
print(np.char.rstrip(str1))

Output:

Original string:  Python NumPy   
After trailing whitepsace are removed:
Python NumPy

In the above example the np.char.rstrip() function returns a new string with trailing whitespace characters removed from the end of the string. It takes a string as its argument and can also take an optional chars argument that specifies which characters to remove from the end of the string.

Pictorial Presentation:

NumPy String operation: rstrip() function

Python - NumPy Code Editor:

Previous: rsplit()
Next: split()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/string-operations/rstrip.php