There’s no better way to demonstrate your mad hacker skills than by replacing letters in your text with numbers: m4d h4x0r 5k1llz!!! This word program automatically converts plain English into leetspeak, the coolest way to talk online. Or at least it was in 1993.
It takes a while to get used to, but with some practice, you’ll eventually be able to read leetspeak fluently. For example, 1t +@]<3s 4 w|-|1le +o g37 |_|s3|) 70, b|_|+ y0u (an 3\/3nt|_|/-\lly r3a|) l33t$peak phl|_|3n+ly
. Leetspeak may be hard to read at first, but the program itself is simple and good for beginners. More information about leetspeak can be found at https://en.wikipedia.org/wiki/Leet.
When you run leetspeak.py, the output will look like this:
L3375P34]< (leetspeek)
By Al Sweigart [email protected]
Enter your leet message:
> I am a leet hacker. Fear my mad skills. The 90s were over two decades ago.
! @m a l33t h@(]<er. ph3@r my m4|) $k|ll$. +h3 90s w3r3 0ver tw0 d3(ad3$ 4g0.
(Copied leetspeak to clipboard.)
The dictionary in the charMapping
variable on line 36 maps plain English characters to leetspeak characters. However, since there can be multiple possible leetspeak characters (such as '7'
or '+'
for the letter 't'
), each value in the charMapping
dictionary is a list of strings. When creating the new leetspeak string, the program has a 30 percent chance of simply using the character in the original English message and a 70 percent chance of using one of the leetspeak characters. This means the same English message has multiple possible leetspeak translations.
1. """Leetspeak, by Al Sweigart [email protected]
2. Translates English messages into l33t5p34]<.
3. This code is available at https://nostarch.com/big-book-small-python-programming
4. Tags: tiny, beginner, word"""
5.
6. import random
7.
8. try:
9. import pyperclip # pyperclip copies text to the clipboard.
10. except ImportError:
11. pass # If pyperclip is not installed, do nothing. It's no big deal.
12.
13.
14. def main():
15. print('''L3375P34]< (leetspeek)
16. By Al Sweigart [email protected]
17.
18. Enter your leet message:''')
19. english = input('> ')
20. print()
21. leetspeak = englishToLeetspeak(english)
22. print(leetspeak)
23.
24. try:
25. # Trying to use pyperclip will raise a NameError exception if
26. # it wasn't imported:
27. pyperclip.copy(leetspeak)
28. print('(Copied leetspeak to clipboard.)')
29. except NameError:
30. pass # Do nothing if pyperclip wasn't installed.
31.
32.
33. def englishToLeetspeak(message):
34. """Convert the English string in message and return leetspeak."""
35. # Make sure all the keys in `charMapping` are lowercase.
36. charMapping = {
37. 'a': ['4', '@', '/-\\'], 'c': ['('], 'd': ['|)'], 'e': ['3'],
38. 'f': ['ph'], 'h': [']-[', '|-|'], 'i': ['1', '!', '|'], 'k': [']<'],
39. 'o': ['0'], 's': ['$', '5'], 't': ['7', '+'], 'u': ['|_|'],
40. 'v': ['\\/']}
41. leetspeak = ''
42. for char in message: # Check each character:
43. # There is a 70% chance we change the character to leetspeak.
44. if char.lower() in charMapping and random.random() <= 0.70:
45. possibleLeetReplacements = charMapping[char.lower()]
46. leetReplacement = random.choice(possibleLeetReplacements)
47. leetspeak = leetspeak + leetReplacement
48. else:
49. # Don't translate this character:
50. leetspeak = leetspeak + char
51. return leetspeak
52.
53.
54. # If this program was run (instead of imported), run the game:
55. if __name__ == '__main__':
56. main()
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
charMapping
dictionary so that it supports new leetspeak characters.Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
return leetspeak
on line 51 to return message
?char.lower()
on line 44 to char
?char.lower()
on line 44 to char.upper()
?leetspeak = leetspeak + leetReplacement
on line 47 to leetspeak = leetReplacement
?