Difference between revisions of "Python String Exercises"

From WLCS
(Exercises)
Line 81: Line 81:
 
#* Using string slicing, print out the word "lazy"
 
#* Using string slicing, print out the word "lazy"
 
#* Using string slicing, print out starting at the word "jumped" through the end of the string
 
#* Using string slicing, print out starting at the word "jumped" through the end of the string
# Use the string variable s from Question 11.  Create a loop that prints out every letter in the variable s
+
# Use the string variable '''s''' from Question 11.  Create a loop that prints out every letter in the variable s
# Use the string variable s from Question 11.  Create a loop that prints out every letter in s backwards starting from the end.
+
# Use the string variable '''s''' from Question 11.  Create a loop that prints out every letter in s backwards starting from the end.
# Use the string variable s from Question 11. Create a loop that prints out every other letter in s starting from the beginning
+
# Use the string variable '''s''' from Question 11. Create a loop that prints out every other letter in s starting from the beginning
  
 
'''Submit:'''
 
'''Submit:'''
 
* Share the document with Mr. Bui
 
* Share the document with Mr. Bui

Revision as of 17:47, 4 December 2012

Objectives

  • You will learn to create and access characters of a string
  • You will learn to access slices of a string
  • You will learn to use a loop to traverse the characters of a string

Directions

  1. Open a Google Doc
  2. Title the file: Python String Exercises
  3. Put your name at the top
  4. Complete the exercises below in this file
  5. Be sure to number each of your answers!!
  6. Share the document with Mr. Bui

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:

  1. schoolName[0]
  2. schoolName[6]
  3. schoolName[3:8]
  4. schoolName[:10]
  5. schoolName[15:]
  6. schoolName[19]
  7. len(schoolName)
  8. len("")
  9. What range specifies “Lee”?
  10. If you had a variable called sample, how could you return the last character, even if you didn't know its length?
  11. 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
  12. Use the string variable s from Question 11. Create a loop that prints out every letter in the variable s
  13. Use the string variable s from Question 11. Create a loop that prints out every letter in s backwards starting from the end.
  14. Use the string variable s from Question 11. Create a loop that prints out every other letter in s starting from the beginning

Submit:

  • Share the document with Mr. Bui