concept str.translate ( ) method in category python

appears as: str.trslate() method
Tiny Python Projects: Learn coding and testing with puzzles and games

This is an excerpt from Manning's book Tiny Python Projects: Learn coding and testing with puzzles and games.

We also looked at the str.translate() method in chapter 4. There we created a dictionary that described how to turn one character, like “1,” into another string like “9.” Any character not mentioned in the dictionary was left alone.

Method 3: Using the str.translate() method

Can we use the str.translate() method to solve this? I showed in chapter 4 how you could use a dictionary called jumper to change a character like “1” to the character “9.” In this problem, we need to change all the lower- and uppercase vowels (10 total) to some given vowel. For instance, to change all the vowels into the letter “o,” we could create a translation table t like so:

t = {'a': 'o',
     'e': 'o',
     'i': 'o',
     'o': 'o',
     'u': 'o',
     'A': 'O',
     'E': 'O',
     'I': 'O',
     'O': 'O',
     'U': 'O'}

We could use t with the str.translate() method: