Python Forum

Full Version: .maketrans() - a piece of code which needs some explanation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I want to understand the following piece of code.
I hope you can help me with it.

import string as str

punc = str.punctuation
string = 'ramdomforthismoment'

table_trans = string.maketrans('', '', str.punctuation)
print(table_trans)
My question is - what the line where the variable table_trans is defined does?
I mean, I read about .maketrans() and it can take 1, 2 or 3 arguments. When 1, it has to be a dictionary 1-1:key-value, for 2 arguments - two strings with equal length, for 3 - strings with different length.
So, here we have 2 strings, but with different length - how does it work? I can't see it.

We obtain mapping each char to None.
for k, v in table_trans.items():
    print(k, v)
But, I don't get it - I don't understand how it is done, this mechanism of mapping a char into the None value.
I would be grateful if anyone would spend their time and explain it to me.
(Jan-28-2021, 04:31 PM)InputOutput007 Wrote: [ -> ]So, here we have 2 strings, but with different length - how does it work? I can't see it.

well, you have 3 strings:
  • empty string ''
  • empty string ''
  • string.punctuations

as described in the docs, first two are of same len and the third argument are the chars mapped to None

and by the way don't use str as alias for string - it's a built-in function/type
(Jan-28-2021, 04:43 PM)buran Wrote: [ -> ]
(Jan-28-2021, 04:31 PM)InputOutput007 Wrote: [ -> ]So, here we have 2 strings, but with different length - how does it work? I can't see it.

well, you have 3 strings:
  • empty string ''
  • empty string ''
  • string.punctuations


and by the way don't use str as shortname for string - it's a built-in function/type

@buran - OMG, I am blind sometimes xD However it is still questionable for me. Why do we have to have 3 strings here? If we wanted to map punctuation chars into None, do we have to have 3 strings? Can we do it differently?
(Jan-28-2021, 04:44 PM)InputOutput007 Wrote: [ -> ]Why do we have to have 3 strings here? If we wanted to map punctuation chars into None, do we have to have 3 strings? Can we do it differently?
That's the easiest way to map multiple chars (in this case every char from string.punctuations) to None. The other way is to pass a dict where each char is key mapped to None.
(Jan-28-2021, 04:47 PM)buran Wrote: [ -> ]
(Jan-28-2021, 04:44 PM)InputOutput007 Wrote: [ -> ]Why do we have to have 3 strings here? If we wanted to map punctuation chars into None, do we have to have 3 strings? Can we do it differently?
That's the easiest way to map multiple chars (in this case every char from string.punctuations) to None. The other way is to pass a dict where each char is key mapped to None.

@buran - thank you
Do we have a similar way for handling with unwanted whitespaces?
If we used this code, we would obtain a text without punctuation chars, but if that text had a lot of whitespaces, they remain after.
You can concatenate string.whitespace and string.punctuations


Or if you want to remove leading/trailing whitespaces you can use rstrip/lstrip/strip methods without argument.