Everyone knows that when you perform import this, in your Python script, you get a printout of The Zen of Python, by Tim Peters.
However, what else is there in the this module?
>>> import this The Zen of Python, by Tim Peters . . . >>> help(this) Help on module this: NAME this MODULE REFERENCE https://docs.python.org/3.8/library/this . . . DATA c = 97 d = {'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', ... i = 25 s = "Gur Mra bs Clguba, ol Gvz Crgref\n\nOrnhgvshy vf o... FILE ...
It looks like The Zen of Python text is stored in the module as this.s in ROT-13 encrypted form. The table for decoding this text is stored in this.d.
What is the most Pythonic way to decode the text in this.s?
- Loop over the characters in this.s:
[something(c) for c in this.s]
- Decode c if it is encoded (is in the this.d dictionary):
[(this.d[c] if c in this.d else c) for c in this.s]
- The result of the comprehension is a list of characters. Make a string out of it:
''.join([(this.d[c] if c in this.d else c) for c in this.s])
- Now, print the result:
print(''.join([(this.d[c] if c in this.d else c) for c in this.s]))
That’s all, folks!