Harold Nelson
9/22/2020
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.
Compute and print the length of the string my_name.
Extract the last character of my_name and print it. Use a positive index.
## 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.
Use a while loop to print each character of my_name.
## H
## a
## r
## o
## l
## d
##
## N
## e
## l
## s
## o
## n
Use a for loop and the in operator to repeat the exercise above.
## 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.
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.
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.
## 6
## Harold
## Nelson
Use string functions to do each of the following and print the results.
Create lower_name, your name in lower case letters.
Create cap_name, your name capitalized, starting from name_lower.
create upper_name, your name in upper case, starting from lower_name.
## harold nelson
## Harold nelson
## HAROLD NELSON
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.
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.
def check(name):
result = name.endswith('son') or name.endswith('sen')
return result
check("Johnson")
## True
## False
## True
Try the following code in Cocalc and observe what happens. It attempts to capitalize a string.
Try the following code in Cocalc and see what it does.
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.
You can’t change a string object.
You can re-use a name to point to a different object.
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.
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.