DEV Community

Discussion on: Python tips and trick, you haven't already seen

Collapse
 
yawpitch profile image
Michael Morehouse

The first example can be made slightly more friendly with str.maketrans:

character_map = str.maketrans({
    '\n': ' ',
    '\t': ' ',
    '\r': None,
})

There's also a shorter form, though it helps to read help(str.maketrans) to see why it works:

character_map = str.maketrans("\n\t", "  ", "\r")