Activity: Strings šŸ§¶ and chars

  1. Create a file called demo_strings.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 next to 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.
  5. Add code that assigns the string ā€œfantasticā€ to the variable f.
    1. Use a for loop to print out each individual character of the string on a separate line.
    2. Use the slice operator on f to print out the last 5 characters of f.
    3. Use the slice operator on f to print out the substring ā€˜tasticā€™.
  6. Add the following lines of code. When you run the program, you will be prompted to type something in the shell. Type anything you would like in the shell, and then press enter. What is printed?
    s = input('Type something here: ') # <-- do not change this part
    print(s)
    
    1. Use the slice operator on s to print only the first 5 characters from the string the user typed in.
    2. Print the number of characters of s using the len function.
  7. Search Project Gutenberg for a book that you like. Choose to view the book as plain-text in your web browser. Highlight all text (Mac: cmd + A, Windows: ctrl + A) and copy it (Mac: cmd + C, Windows: ctrl + C). Back in Thonny, create a variable called book and assign it the string you copied. Itā€™s a very long string, so we will use notation to start and end a multiline string ("""). For example:
    book = """
    put all pasted text here
    ....
    it will span many lines
    all of this is still a string
    """
    
  8. Use string functions on the string variable book to answer the following questions. Print your results.
    1. How often does the word ā€˜theā€™ occur? What about ā€˜scienceā€™?
    2. How often does the character ā€˜qā€™ occur? What about ā€˜sā€™?
    3. How many characters long is the book?
    4. What other questions can you answer?

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ā€ (note the upper and lowercase characters). 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 python file to Moodle.