CS 100

Logo

Scientific Computing

Activity 14 - More practice with strings

Create a file named activity14.py and save it to your cs100/ch3 directory.

  1. Write a function countCharacterTypes(myString) that takes a string as a parameter. It will count and print the number of uppercase characters, lowercase characters, digits, and symbols (non-alphanumeric characers). Test your function by calling it. For example, calling the function countCharacterTypes("Here.123 is EXAMPLE !!^&*") should print out:
    Number of uppercase characters: 8
    Number of lowercase characters: 5
    Number of digit characters: 3
    Number of symbol characters: 9
    
  2. Modify the previous function so that the output is nicely formatted. (Hint: think about justification). Test your function by calling it. The digits should line up as follows:
     Number of uppercase characters: 8
     Number of lowercase characters: 5
         Number of digit characters: 3
        Number of symbol characters: 9
    
  3. Write a function allOccurrences(myString, character) which takes a string as the first parameter and a single character as the second parameter. It should print all indices where the given character is found in myString. Hint: use a for loop. Test your function by calling it. For example, the function call allOccurrences("Here is an example string", 'e') would produce the following output:
    1
    3
    11
    17
    

If you finish early

  1. Write a function reverseString(myString) to reverse the given string and return it. Use a for loop to solve this problem. Test your function. For example, print(reverseString("scientific computing")) will output gnitupmoc cifitneics.

How to submit

Submit your working python file to Moodle.