Lecture 14 - Exercises

14A - String Methods

  1. What do each of the following expressions evaluate to?

    1. "Wow".replace("W", "t").upper()
    2. "banana".find("a")
    3. " O_O ".strip().replace("_", "-")
    4. "banana".count("a")
  2. Write a single expression that evaluates to the number of occurrences of the letter “E”, regardless of case, in a string s.

14B - String Comparisons and Ordering

  1. Evaluate the following expressions:

    1. "To be or not to be".find("be") == 4
    2. "Boo".replace("o", "O").lower() <= "boo"
    3. "no" in "To be or not to be"
    4. "stark" not in "Tony Stark"
    5. "bat" > "rat"
    6. "tap" < "bear"
    7. "Jam" < "bet"
    8. "STEAM" > "STEP!"
  2. What does the following program print?

Problems

  1. Implement the following function, but this time use string methods to do it without a loop:

  2. Implement the following function, but this time use string methods to do it without a loop:

  3. Implement the following function:

  4. Implement the following function:

  5. Read up on the format string method. Write a program that implements a “Mad Libs” style word game (see here if you’re not familiar with Mad Libs). First, prompt the user for a series of words, each a specific part of speech. Then fill the words into a story of your choice and print the story. Implement this program with only one call to the print function with only one argument. You may find it helpful to use triple-quoted strings so you can write your story on multiple lines.

  6. Write a program to print a tidy, well-aligned table of characters and their corresponding character codes, starting with 32 and ending at 126. Make the character codes count by one going down each column. For extra fun, take a single command line argument that specifies how many columns your table should have. Example outputs:

    >>> %Run asciitable.py 6
     32     48 0   64 @   80 P   96 `  112 p  
     33 !   49 1   65 A   81 Q   97 a  113 q  
     34 "   50 2   66 B   82 R   98 b  114 r  
     35 #   51 3   67 C   83 S   99 c  115 s  
     36 $   52 4   68 D   84 T  100 d  116 t  
     37 %   53 5   69 E   85 U  101 e  117 u  
     38 &   54 6   70 F   86 V  102 f  118 v  
     39 '   55 7   71 G   87 W  103 g  119 w  
     40 (   56 8   72 H   88 X  104 h  120 x  
     41 )   57 9   73 I   89 Y  105 i  121 y  
     42 *   58 :   74 J   90 Z  106 j  122 z  
     43 +   59 ;   75 K   91 [  107 k  123 {  
     44 ,   60 <   76 L   92 \  108 l  124 |  
     45 -   61 =   77 M   93 ]  109 m  125 }  
     46 .   62 >   78 N   94 ^  110 n  126 ~  
     47 /   63 ?   79 O   95 _  111 o  
    >>> %Run asciitable.py 9
     32     43 +   54 6   65 A   76 L   87 W   98 b  109 m  120 x  
     33 !   44 ,   55 7   66 B   77 M   88 X   99 c  110 n  121 y  
     34 "   45 -   56 8   67 C   78 N   89 Y  100 d  111 o  122 z  
     35 #   46 .   57 9   68 D   79 O   90 Z  101 e  112 p  123 {  
     36 $   47 /   58 :   69 E   80 P   91 [  102 f  113 q  124 |  
     37 %   48 0   59 ;   70 F   81 Q   92 \  103 g  114 r  125 }  
     38 &   49 1   60 <   71 G   82 R   93 ]  104 h  115 s  126 ~  
     39 '   50 2   61 =   72 H   83 S   94 ^  105 i  116 t  
     40 (   51 3   62 >   73 I   84 T   95 _  106 j  117 u  
     41 )   52 4   63 ?   74 J   85 U   96 `  107 k  118 v  
     42 *   53 5   64 @   75 K   86 V   97 a  108 l  119 w