CS 115 Lab 7: Setup and Practice

[Back to lab instructions]

Refer to the Week 7 reading or other resources as necessary to complete this assignment.


Setup

  1. If the machine was on when you started using it, reboot it to MacOS.
  2. Follow these procedures to mount your blue home-directory on the local machine and create a PyCharm project (call it Lab07). You will use PyCharm in the next part, but, it is important that you set up your project now so that if you run into any issue, we can resolve them quickly.

Practice part A: Strings

Answer Question 1 (the vocabulary question) in your writeup.

Then, go to the online Python 3 tutor to complete the remaining practice questions. You should try to predict the answers before putting them into the online tutor.

These questions cover string indexing, concatenation, repetition, slicing, and justification.

  1. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(x[2])
    
    Predict what this code will do and answer Question 2 in your writeup.
  2. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(x[2] + y[1])
    
    Predict what this code will do and answer Question 3 in your writeup.
  3. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(x + y)
    
    Predict what this code will do and answer Question 4 in your writeup.
  4. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(x + y)
    print(x)
    
    Predict what this code will do and answer Question 5 in your writeup.
  5. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(2 * y)
    
    Predict what this code will do and answer Question 6 in your writeup.
  6. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(y[1:5])
    
    Predict what this code will do and answer Question 7 in your writeup.
  7. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(y[:5])
    
    Predict what this code will do and answer Question 8 in your writeup.
  8. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(y[1:])
    
    Predict what this code will do and answer Question 9 in your writeup.
  9. For the following code...
    x = 'I am a string'
    y = 'me too'
    print(y[:])
    
    Predict what this code will do and answer Question 10 in your writeup.

Practice part B: String methods; lists of strings

  1. For the following code...
    x = 'I am a string'
    print(x.upper())
    
    Predict what this code will do and answer Question 11 in your writeup.
  2. For the following code...
    x = 'I am a string'
    print(x.lower())
    print(x)
    
    Predict what this code will do and answer Questions 12 and 13 in your writeup.
  3. For the following code...
    x = 'I am a string'
    z = x.split()
    print(z[0])
    print(x)
    
    Predict what this code will do and answer Questions 14 and 15 in your writeup.
  4. For the following code...
    x = 'I am a string'
    for w in x.split():
        print(w, end="")
    print()
    
    Predict what this code will do and answer Question 16 in your writeup.
  5. For the following code...
    x = 'I am a string'
    y = x.split()
    print(len(y))
    
    Predict what this code will do and answer Question 17 in your writeup.