The char.translate()
function in Python is used to return a copy of the string from a given input array whereby all the characters occurring in the optional argument deletechars
are removed, and the remaining characters are mapped through the given table translation.
char.translate(a, table, deletechars=None)
The char.translate()
function takes the following parameter values:
a
: This is the input array of strings or Unicode.table
: This is the translate mapping that is specified to perform the translations.deletechars
: This is the string character that needs to be removed.The char.translate()
function returns an array of strings or Unicode, depending on the input type that is passed to the function.
import numpy as np# creating an arraymyarray = np.array(["That", "There", "The"])# creating a dictionary for the translation tablemydictionary = {"T": "B", "h": "l", "e": "a", "a": "o"}# creating the translation tablemytable = "Thea".maketrans(mydictionary)#printing the original arrayprint(myarray)# implementing the char.translate() functionprint(np.char.translate(myarray, mytable, deletechars=None))
numpy
module.myarray
, using the array()
function.mydictionary
for the table
parameter for translations.maketrans()
method on the dictionary mydictionary
.myarray
.char.translate()
function on the array myarray
by passing mytable
as the table parameter value. Then, we print the result to the console.