Exercises on Strings

Harold Nelson

1/30/2020

Exercise 1

Create a string, my_name, containing your name including your first name and last name separated by a single blank space. Then put the second character of your name in the variable second and print second.

my_name = "Harold Nelson"
second = my_name[1]
print(second)
## a

Exercise 2

Compute and print the length of the string my_name.

Answer

print(len(my_name))
## 13

Exercise 3

Extract the last character of my_name and print it.

Answer

print(my_name[12])
## n

Note that you need to use one less than the length of the string to reference its last character. This is a consequence of indexing from 0.

Exercise 4

Use a while loop to print each character of my_name.

Answer

length = len(my_name)
pos = 0
while pos <  length:
    print(my_name[pos])
    pos = pos + 1
## H
## a
## r
## o
## l
## d
##  
## N
## e
## l
## s
## o
## n

Exercise 5

Use a for loop and the in operator to repeat the exercise above.

Answer

for letter in my_name:
     print(letter)
## H
## a
## r
## o
## l
## d
##  
## N
## e
## l
## s
## o
## n

Note that the definite loop is much easier since you don’t need to think about indexing.

Exercise 6

Create a and print a string straddle consisting of the last character of your first name, the blank space, and the first character of your last name. Use slicing.

Ans

straddle = my_name[5:8]
print(straddle)
## d N

Exercise 7

Manually locate the index of the blank space and call it blank_pos. Use this variable to get strings first_name and last_name using slice specifications with only one number. Print the values of blank_pos, first_name and last_name.

Answer

blank_pos = 6
first_name = my_name[:blank_pos]
last_name = my_name[blank_pos + 1:]
print(blank_pos)
## 6
print(first_name)
## Harold
print(last_name)
## Nelson

Exercise 8

Use string functions to do each of the following and print the results.

lower_name = my_name.lower()
print(lower_name)
## harold nelson
cap_name = lower_name.capitalize()
print(cap_name)
## Harold nelson
upper_name = lower_name.upper()
print(upper_name)
## HAROLD NELSON

Exercise 9

Used find() to locate the position of the blank space in my_name as blank_pos_2. Test for the equality of blank_pos and blank_pos_2.

Answer

blank_pos_2 = my_name.find(' ')
print(blank_pos == blank_pos_2)
## True

Exercise 10

Create a function check which returns True if a name ends with ‘sen’ or ‘son’ and returns False otherwise. Test your function with the names Johnson, Smith and Andersen.

Answer

def check(name):
    result = name.endswith('son') or name.endswith('sen')
    return result
check("Johnson")  
## True
check("Smith")
## False
check("Andersen")
## True

Comment on Immutability

One salient characteristic of strings is that they are immutable. Once a string is created, it’s value can’t be changed.

Exercise 11

Try the following code in Cocalc and observe what happens. It attempts to capitalize a string.

s = "abc"
s[0] = "A"
print(s)

Exercise 12

Try the following code in Cocalc and see what it does.

s = "abc"
s = "A" + s[1:]
print(s)

Huh

Did I get away with violating immutability?

https://realpython.com/python-variables/ is worth reading.

Here’s the bottom line. There are three distinct things.

  1. Name
  2. Object
  3. Value

You can’t change a string object.

You can re-use a name to point to a different object.

Exercise 13

This point enables us to use our accumulation pattern to build a string by starting with an empty string and adding to it by concatenation. Use this fact to write a program to start with the string “Use this fact” and build a string consisting of all the vowels in the original string.

Note: Start by creating a string consisting of all the vowels, both uppercase and lowercase. Remember that the operator “in” can test for the presence of a character in a string.

Answer

vowels = "aeiouAEIOU"
s = "Use this fact"
res = ""
for i in s:
    if i in vowels:
        res = res + i
print(res)        
## Ueia