Python Forum
.maketrans() - a piece of code which needs some explanation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.maketrans() - a piece of code which needs some explanation
#1
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.
Reply
#2
(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
InputOutput007 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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?
Reply
#4
(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.
InputOutput007 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
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.
InputOutput007 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explanation of code ejKDE 4 309 Feb-26-2024, 02:50 PM
Last Post: ejKDE
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 657 Sep-05-2023, 12:50 PM
Last Post: ToniE
  First piece of code Bearwulf 2 723 Aug-02-2023, 04:03 PM
Last Post: deanhystad
  A better explanation of the last post Led_Zeppelin 9 2,323 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Understanding a piece of code Michael1 4 1,374 Jan-20-2022, 07:14 PM
Last Post: Michael1
  How to make this piece concise leoahum 0 1,303 Sep-23-2021, 09:23 PM
Last Post: leoahum
  Operator meaning explanation Sherine 3 1,980 Jul-31-2021, 11:05 AM
Last Post: Sherine
  Explanation of except ... as : Fernando_7obink 2 1,887 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .remove() from a list - request for explanation InputOutput007 3 2,178 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Explanation of the left side of this statement please rascalsailor 3 2,454 Sep-09-2020, 02:02 PM
Last Post: rascalsailor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020