operations with empty strings | translate () function

numpy.core.defchararray.translate (arr, table, deletechars = None) — another function to do string operations in numpy. For each item in arr, it returns a copy of the string in which all characters occurring in the optional deletechars argument are removed and the rest of the characters are displayed in this translation table. If more than one value needs to be translated, a dictionary is passed to the maketrans function to create the translation table.

arr: array_like of str or unicode.Input array.
table: Translate mapping specified to perform translations.
deletechars: String type, characters to be deleted.

Returns: [ndarray] Output array of str or unicode with translated values.

Code # 1:

# Python program explaining
# numpy.core.defchararray.translate () method

# numpy import

import numpy as geek

# input array

in_arr = geek.array ([ `Weeks` , `our` , ` Weeks` ])

print ( " Input original array: " , in_arr) 

# create a dictionary for the translation table

trans_dict = { "W" : "G" , "o" : "f" , "u" : "o"

# create translation table from dictionary

trans_table = "Wou" .maketrans (trans_dict) 


out_arr = geek.core.defchararray.translate (in_arr, trans_table, deletechars = "None" )

print ( "Output translated array:" , out_arr) 


 Input original array: [`Weeks`` our` `Weeks`] Output translated arra y: [`Geeks`` for` `Geeks`]