CS 100

Logo

Scientific Computing

Activity 14 - Strings 🧶 and chars

  1. Create a file called activity14.py and save it to your cs100/ch3 folder.
  2. At the top of the file, type myCourse = "CS100 Scientific Computing". For the following questions, write code to check your answer, and write your answer in comments above it.
    1. What Python command will print the length of the variable myCourse?
    2. What is the following command printing? print( myCourse[-9:-5])
    3. What is the following command printing? print( myCourse[17:21])
    4. What is the following command printing? print( myCourse[17] )?
    5. What is the index of the first character 'S' in myCourse, and what command do you use to find it?
    6. What is the result of typing print('100' in myCourse)?
    7. What is the result of typing print('130' not in myCourse)?
    8. What is the result of typing print('wow'*5)?
    9. What is the result of typing print( ('.'*3 + 'y')*3 )?
  3. Write code that concatenates myCourse with "Exam 1".
  4. Write code that prints the myCourse string so that it is center justified and takes up 40 characters of space. Hint: use the center function.

If you finish early

  1. Application in Biology! Write a function countACGT(seq) that displays the number of a, c, g, and t characters that occur in a DNA sequence. Call your function to test it with the string “aactTtgttAct”. For example, calling the function countACGT('aactTtgttAct') should display:
    No. of A: 3
    No. of C: 2
    No. of G: 1
    No. of T: 6
    Other letters: 0
    

How to submit

Submit your working activity14.py to Moodle.