+ 1

Can anyone help me to understand the use of "translate" function with the aid of an example?

Can anyone help me to understand the use of "translate" function with the aid of an example?

1st Jan 2021, 10:23 AM
Atul [Inactive]
7 ответов
+ 3
Not necessarrily, you can use maketrans() (make translation) method to create an ascii dictionary order with using character parameters. For Example: txt = "Hello Sam!" mytable = txt.maketrans("S", "P") print(txt.translate(mytable)) >> Hello Pam! - - - - - - - - - - - - - - - - - - Maketrans creates a single pair dictionary of ascii order of the two given parameter (ord(S) will be the key and ord(P) will be the value). Then assigns it to "mytable" variable, and then so you can use it with translate method. - - - - - - - - - - - - - - - - - - # This is also the same with txt = "Hello Sam!" ans = txt.replace("S", "P") print(ans) >> Hello Pam! - - - - - - - - - - - - - - - - - - Their difference is that, translate() is more flexible than replace() when it comes to Non-Ascii characters like Non-english alphabet or characters.
1st Jan 2021, 10:44 AM
noteve
noteve - avatar
+ 3
As far as I know, translate is commonly used with maketrans() method. But wait I'll send you some helpful links.
1st Jan 2021, 10:35 AM
noteve
noteve - avatar
+ 2
It is a string method that translates the given ascii order into character just like "ord" and "chr" but in different format. See this for example. Based on my understanding, it is similar to string.replace() function except that in translate method, you need to have a dictionary which the key represents the ascii order of charcater to be replaced and the value represents the ascii order of the character that you will replace. https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_translate.asp
1st Jan 2021, 10:38 AM
noteve
noteve - avatar
+ 2
Thank you
1st Jan 2021, 10:54 AM
Atul [Inactive]
0
I am asking about the "translate" function ( an inbuilt method in Python) . It's meaning and use with an example
1st Jan 2021, 10:34 AM
Atul [Inactive]
0
Sir is it necessary to input only the ASCII values?
1st Jan 2021, 10:42 AM
Atul [Inactive]