Unit 1

String indexing, slicing, looping

Code Writing

[10.1] You are given a string s. How would you:

  1. Get the first letter of the string
  2. Get the last letter of the string
  3. Get the 5th letter of the string (assuming the string has at least 6 letters)

[10.2] You are given the string s = “My name is Mira”.

  1. Get the substring “name” and store it into a variable.
  2. Get the substring “Mira” and store it into a variable.
  3. Now create a new string that says “name: Mira”.
  4. Now create a new string that says “NAME: MIRA”.

[10.3] Print a string that uses double quotation marks inside the string.

[10.4] Print a string that uses an apostrophe inside the string.

[10.5] Print the string “zing” by using slice notation on the string “bazinga” to specify the correct range of characters.

[10.6] Create two strings, concatenate them and then print the resulting string.

[10.7] Create a string variable. Print the string and its length

[10.8] Create the string “aweawesomesome” from the given string s=”awesome”.

[10.9] Modify the above code so that the printed string is reversed.

[10.10] Given two strings, s1 and s2. Write a few lines of code to create a new string s3 by appending s2 in the middle of s1. If the string length is odd, the end of the string will have the extra letter. For example, if the length of s1 was 13, s3 would have 6 characters of s1, then s2, then 7 characters of s1.

[10.11] Write a few lines of code that converts the following strings to lowercase: "Animals", "Badger", "Honey Bee", "Honey Badger".

[10.12] Repeat the above exercise, but convert each string to uppercase instead of lowercase.

[10.13] Write a few lines of code that removes whitespace from the following strings, then print out the strings with the whitespace removed:


string1 = " FiletMignon" 
string2 = "Brisket " 
string3 = " Cheeseburger "

[10.14] Write the function checkTwos that takes in a string as a parameter and returns True if the second character and the second to last character of the string are the same. You are guaranteed that the string will be at least 4 characters long.

[10.15] Given a string of numbers and characters, write the function numsOnly that returns all the numbers in the string in the same order they occur (in string form).

[10.16] Write a function matchString that takes in two strings, one a sentence and a word, and returns how many times the word occurs in the sentence. Be careful of casing - we are counting the word regardless of if it is lowercase vs uppercase.

[10.17] Write the function numFun that takes a string as a paramter. The function should print the number of digits in the string and should return the average of these numbers.

[10.18] Write the function splicedCheck that takes in two indexes and a string as a paramter and returns the substring obtained by slicing the string starting from the first given index up to and including the character of the second index. Remember spaces also have associated indexes!

[10.19] Write the function count_characters that takes input from the user and displays the number of characters in the given input.

[10.20] Write the function not_string that takes a string as a paramter and returns a new string where the string "not " has been added to the front. However, if the string already begins with "not", return the string unchanged.

[10.21] Write the function count_characters that takes a string as a parameter and counts and prints all letters, digits, and special symbols from the given string.

[10.22] Write a function count_vowels that takes a sentence as paramter and counts the number of vowels in the sentence (a, e, i, o, u). Count for both upper and lower case vowels.

Code Tracing

[10.23] What is the output of the following code?


s = 'abcdef'
t = s[0] + s[5] + s[len(s) - 5]

print(t)

[10.24] Trace through this code. What is printed at the end, once all the code is executed?


s1 = "I"
s2 = s1
print(s2 + " ")
s1 = "LOCO"
s3 = "DINGVE"
print(s1[0:2] + s3[4:len(s3)] + " ")
s4 = s3[0:4] + "!"
print(s4)

[10.25] Trace through this code below. What is printed at the end?


s = "hi my name is"
for i in range(1, len(s)):
    s[i] = s[i-1]
print(s[1:])

[10.26] Trace through the code below. What does the function call return? What is printed?


def mysteryStringFun(s1, n):
    new_s = ""
    for i in range(n):
        new_s += s1[i] * i
    return new_s + s1[n-1:]

some_s = "CMU is fun!"
print("original: ", some_s)
print("function returns: ", mysteryStringFun(some_s, 5))

[10.27] Trace through the code below. What does it print?


meal = "cheesecake, steak, fries"
for i in range(0, len(meal), 2):
    print(meal[i])

[10.28] What will be the output of the following code?


word = "python is awesome"
index = 0
while index < len(word):
    print(word[index])
    index += 2