Once again, this week I will ask you to work with a partner of your choice. You will turn in a SINGLE answer sheet for both partners. As a reminder, you should...
Failure to follow these guidelines may cause you to lose points for this activity.
Yesterday in class we spent a little bit of time looking at String as a data structure and some of the initial things you can do to the String. Let's review:
At the command prompt type
[Q1] What happens when you invoke each of these commands: (be careful with the last one)>>> greet = "Have a great day!"
Command | Results |
len(greet) | |
greet[1] | |
greet[7] | |
greet[1:8] | |
greet[:8] | |
greet[8:] | |
greet[13]+greet[10:12]+greet[14] | |
greet[0]*3 | |
(greet[0]+greet[5:7])*3 |
[Q2] What commands would you type (using greet from above) to get the following results
Command | Results |
t | |
! | |
the last letter of ANY message (While you may use subtraction, you should NOT use the negative operators, like greet[-1]) | |
great da | |
great great dada | |
yard |
[Q3] What happens when you invoke each of these commands:
Command | Results |
greet[-1] | |
greet[-4] | |
greet[-11] | |
greet[-7:-3] |
[Q4] What is happening when you use negative numbers in the indexing/slice command?
Suppose that every UNI email address was generated from the following formula:
The first 5 characters of your last name followed by the first initial of your first name followed by @uni.edu
For example:
Write a program called generateEmail.py This program should prompt the user for the first name and then prompt the user for their last name. It should then print that person's email address using the formula described above.
Often times we want to write code that loops through a string. There are several ways to do this.
Enter the text below into an editor window.
Run the code. What happens?
Now try this related, but different block of code
Run this code. What happens?
The assignment
Using either of the blocks of code above as a starting point, write a program called reverseString.py which takes in a String of text entered by the user and prints out the exact same string but in reverse. For example:
PLEASE NOTE, I am asking you to write code to actually build the reversed string. You should not use the "cheat" (legal, but not the point) that your book discusses to do this with only one command.
Write a program called countVowels.py which takes in a string of text entered by the user and uses a for loop to iterate over each character in that string to count the number of EACH VOWEL (a, e, i, o , and u). The program should then print the results of this count. For example
(Note that I am asking YOU to count in your code and not use one of the built in tools in the language which you will look at later in this lab).
SUGGESTION - Begin by figuring out how to make this work to count As. When you are done, add in functionality for the other vowels.
NOTE: Again, I am asking YOU to do this, not use the built in functions of a string.
Final Submission
Please submit your code for electronic grading using the eLearning submission system.
Follow the directions on the system to select the appropriate course and assignment and submit
Don't forget to hand in your answer sheet to the professor or TA before you leave!