String Methods Exercises

  1. With your neighbor, choose one (or more) of the following string methods (preferably one from each category):
    1. String testing methods:
      • isalnum()
      • isalpha()
      • isdigit()
      • islower()
      • isspace()
      • isupper()
    2. String modification methods:
      • lower()
      • lstrip(), lstrip( char )
      • rstrip(), rstrip( char )
      • upper()
    3. String searching and replacing methods:
      • startswith( substring )
      • endswith( substring )
      • find( substring)
      • replace( substring, newString )
    4. Other:
      • split()
    5. Non string methods:
      • int()
      • float()
      • bool()
  2. Look up the method in some Python reference material
  3. Describe the method to your neighbor
  4. Write a python script that:
    1. Demonstrates the original (built-in) method
    2. Uses a function that you wrote that replicates the same behavior, without calling the method
    For example, for the repetition operator, *:
    # replicate str1 * num (without using *)
    def strRepetition( str1, num):
        
        # use newStr to store the result
        newStr = ""
    
        # make a copy of the str1, num times
        for i in range( num):
            newStr += str1
    
        # return the copied string to whomever called this function    
        return newStr
    
    def main():    
        strVar = "Ha"
        num = 5
    
        # show the original behavior of string repetition
        result = strVar * num
        print( strVar, " * ", num, ": ", result, sep='')
    
        # call the replicated method
        result = strRepetition( strVar, num)
        print( "strRepetition(", strVar, ", ", num, "): ", result, sep='')
    
        
    main()    
    
    Output:
    Ha * 5: HaHaHaHaHa
    strRepetition(Ha, 5): HaHaHaHaHa