Spongecase
You’ve probably seen the “Mocking SpongeBob” meme: a picture of SpongeBob SquarePants, with a caption whose text alternates between upper- and lowercase letters to indicate sarcasm, like this: uSiNg SpOnGeBoB MeMeS dOeS NoT mAkE YoU wItTy. For some randomness, the text sometimes doesn’t alternate capitalization.
This short program uses the upper() and lower() string methods to convert your message into “spongecase.” The program is also set up so that other programs can import it as a module with import spongecase and then call the spongecase.englishToSpongecase() function.
spongecase.py
1"""sPoNgEcAsE, by Al Sweigart al@inventwithpython.com
2Translates English messages into sPOnGEcAsE.
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 """Run the Spongecase program."""
16 print('''sPoNgEtExT, bY aL sWeIGaRt Al@iNvEnTwItHpYtHoN.cOm
17
18eNtEr YoUr MeSsAgE:''')
19 spongecase = englishToSpongecase(input('> '))
20 print()
21 print(spongecase)
22
23 try:
24 pyperclip.copy(spongecase)
25 print('(cOpIed SpOnGeCasE to ClIpbOaRd.)')
26 except:
27 pass # Do nothing if pyperclip wasn't installed.
28
29
30def englishToSpongecase(message):
31 """Return the spongecase form of the given string."""
32 spongecase = ''
33 useUpper = False
34
35 for character in message:
36 if not character.isalpha():
37 spongecase += character
38 continue
39
40 if useUpper:
41 spongecase += character.upper()
42 else:
43 spongecase += character.lower()
44
45 # Flip the case, 90% of the time.
46 if random.randint(1, 100) <= 90:
47 useUpper = not useUpper # Flip the case.
48 return spongecase
49
50
51# If this program was run (instead of imported), run the game:
52if __name__ == '__main__':
53 main()