Tuesday, April 7, 2026
HomeLanguagesnumpy string operations | translate() function

numpy string operations | translate() function

numpy.core.defchararray.translate(arr, table, deletechars=None) is another function for doing string operations in numpy. For each element in arr, it returns a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table. If there are more than one values to be translated, a dictionary is passed to maketrans function to create a translation table.

Parameters:
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 
  
# importing numpy 
import numpy as geek
  
# input array 
in_arr = geek.array(['Weeks', 'our', 'Weeks'])
print ("Input original array : ", in_arr) 
  
# creating dictionary for translation table 
trans_dict ={"W": "G", "o": "f", "u": "o"} 
    
# creating 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) 


Output:

Input original array :  ['Weeks' 'our' 'Weeks']
Output translated array:  ['Geeks' 'for' 'Geeks']
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6886 POSTS0 COMMENTS
Nicole Veronica
12007 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12102 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7261 POSTS0 COMMENTS
Thapelo Manthata
6973 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS