L3375P34]<

Tags: tiny, beginner, word

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.

leetspeak.py
 1"""Leetspeak, by Al Sweigart al@inventwithpython.com
 2Translates English messages into l33t5p34]<.
 3This code is available at https://nostarch.com/big-book-small-python-programming
 4Tags: tiny, beginner, word"""
 5
 6import random
 7
 8try:
 9    import pyperclip  # pyperclip copies text to the clipboard.
10except ImportError:
11    pass  # If pyperclip is not installed, do nothing. It's no big deal.
12
13
14def main():
15    print('''L3375P34]< (leetspeek)
16By Al Sweigart al@inventwithpython.com
17
18Enter 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
33def 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:
55if __name__ == '__main__':
56    main()

https://inventwithpython.com/bigbookpython/project40.html