Difference between revisions of "Python String Exercises"
From WLCS
Line 63: | Line 63: | ||
#<code>schoolName[0]</code> | #<code>schoolName[0]</code> | ||
#<code>schoolName[6]</code> | #<code>schoolName[6]</code> | ||
− | |||
#<code>schoolName[3:8]</code> | #<code>schoolName[3:8]</code> | ||
#<code>schoolName[:10]</code> | #<code>schoolName[:10]</code> | ||
Line 69: | Line 68: | ||
#<code>schoolName[19]</code> | #<code>schoolName[19]</code> | ||
#<code>len(schoolName)</code> | #<code>len(schoolName)</code> | ||
− | #<code>len( | + | #<code>len("")</code> |
#What range specifies “Lee”? | #What range specifies “Lee”? | ||
#What expression returns the last character of string ‘sample’? | #What expression returns the last character of string ‘sample’? | ||
Line 84: | Line 83: | ||
'''Submit:''' | '''Submit:''' | ||
− | * Submit | + | * Submit your file to [http://wlhs.schoolweblockers.com/ School Web Lockers]. |
+ | ** Put your first & last name in the filename and at the top of the file. | ||
+ | * Print it out |
Revision as of 15:55, 13 February 2011
Objective
- You will learn to create and access characters and slices of a string
- You will learn to use a loop to traverse the characters of a string
Directions:
- Open a text editor (Applications->Accessories->gEdit Text Editor)
- Put your name at the top
- Save the file as YOURLASTNAME_ch7.txt
- Complete the exercises below in this file. If you use DrPython, then be sure to copy your code back into this file
- Be sure to number each of your answers!!
References:
Exercises
- Questions 1-11, assume you have the following following representation of the string
schoolName = "Washington-Lee H.S."
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
W | a | s | h | i | n | g | t | o | n | - | L | e | e | H | . | S | . |
What is:
schoolName[0]
schoolName[6]
schoolName[3:8]
schoolName[:10]
schoolName[15:]
schoolName[19]
len(schoolName)
len("")
- What range specifies “Lee”?
- What expression returns the last character of string ‘sample’?
- Create a string "The quick brown fox jumped over the lazy dog." in a variable s
- Print out the first letter in s using the brackets (e.g. s[NUM])
- Print out the letter "q" using s and the index 4
- Print out the letter "x" using s and the corresponding index
- Using string slicing, print out the word "brown"
- Using string slicing, print out the word "lazy"
- Using string slicing, print out starting at the word "jumped" through the end of the string
- Use the string variable s from Question 12. Create a loop that prints out every letter in the variable s
- Use the string variable s from Question 12. Create a loop that prints out every letter in s backwards starting from the end.
- Use the string variable s from Question 12. Create a loop that prints out every other letter in s starting from the beginning
Submit:
- Submit your file to School Web Lockers.
- Put your first & last name in the filename and at the top of the file.
- Print it out