Lecture 16 - Exercises

16A - split and join

  1. What does the following print?

    print("_".join("88r4if4r462".split("r4")))

16B - Reading and Writing Files

  1. Suppose the file rick.txt contains:

    Never gonna give you up

    What is the output of the following code?

    print(open("rick.txt", "r").read(5).split("e"))
  2. Assume a file called fin.txt exists in the same directory as the program below, and has the following contents:

    Start: 4pm
    End: 7pm
    3h

    What does the following program print?

    fin = open("fin.txt", 'r')
    fot = open("fot.txt", 'w')
    
    for line in fin:
        fl = line.strip().split(': ')
    
        fot.write(fl[-1])
        fot.write(fl[0])
    
    fot.close()
    fin.close()
    
    fot = open("fot.txt", 'r')
    a = fot.read(3)
    fot.seek(8)
    print(a, fot.read())
    fot.close()