Difference between revisions of "Python String Exercises"

From WLCS
Line 1: Line 1:
== Objective ==
+
'''Objective'''
 
* You will learn to create and access characters and slices of a string
 
* 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
 
* You will learn to use a loop to traverse the characters of a string
  
== Directions ==
+
'''Directions:'''
Create a python file named "ch7_YOURLASTNAME.py"In this file, complete the following exercises:
+
# 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 fileIf you use DrPython, then be sure to copy your code back into this file
 +
# Be sure to number each of your answers!!
  
== Exercises ==
+
'''References:'''
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch07.html How to Think Like a Computer Scientist: Chapter 7]
 +
 
 +
'''Exercises'''
 
Questions 1-11, assume you have the following following representation of the string <code>schoolName = "Washington Lee H.S."</code>
 
Questions 1-11, assume you have the following following representation of the string <code>schoolName = "Washington Lee H.S."</code>
 
{|- border="1"
 
{|- border="1"
Line 72: Line 79:
 
# 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 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
 
# 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 the file to [http://wlhs.schoolweblockers.com/ School Web Lockers]

Revision as of 11:58, 20 January 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:

  1. Open a text editor (Applications->Accessories->gEdit Text Editor)
  2. Put your name at the top
  3. Save the file as YOURLASTNAME_ch7.txt
  4. Complete the exercises below in this file. If you use DrPython, then be sure to copy your code back into this file
  5. 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."

00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18
W a H . S .

What is:

  1. schoolName[0]
  2. schoolName[6]
  3. schoolName[3]=”X”
  4. schoolName[3:8]
  5. schoolName[:10]
  6. schoolName[15:]
  7. schoolName[19]
  8. len(schoolName)
  9. len(“”)
  10. What range specifies “Lee”?
  11. What expression returns the last character of string ‘sample’?
  12. 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
  13. Use the string variable s from Question 12. Create a loop that prints out every letter in the variable s
  14. Use the string variable s from Question 12. Create a loop that prints out every letter in s backwards starting from the end.
  15. Use the string variable s from Question 12. Create a loop that prints out every other letter in s starting from the beginning

Submit: