Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Can someone explain what translate() and maketrans() do in python?

E.g. line = line.translate(line.maketrans('', '', string.punctuation))

Confused to what they do exactly.

1 Answer

Relevance
  • EddieJ
    Lv 7
    1 month ago
    Favorite Answer

    maketrans () can have 2 or 3 arguments.

    In your example, you have 3 arguments, but it might not be obvious because 2 single quotes can look like one double quote.

    The first argument is an empty string that is delimited by single quotes.  The 2nd argument is identical.  So, the primary use of the maketrans () method is being skipped over because there is nothing to translate.

    The 3rd argument contains every punctuation character so that specifies characters to remove from the object of the translate () method, which is <line>.

    So, the overall effect is to remove punctuation characters from <line>.

Still have questions? Get your answers by asking now.