Difference between revisions of "Python String Exercises"
From WLCS
(→Directions) |
|||
| (27 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | == | + | ==Objectives== |
| − | * You will learn to create and access characters | + | * 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 | * You will learn to use a loop to traverse the characters of a string | ||
| − | == Directions == | + | ==Directions== |
| − | + | # Open a Google Doc | |
| + | # Title the file: Python String Exercises | ||
| + | # Put your name at the top | ||
| + | # Complete the exercises below in this file | ||
| + | # Be sure to number each of your answers!! | ||
| + | * Submit the document via APS Canvas | ||
| + | ==References== | ||
| + | * [[Media:Strings_Python.pptx]] | ||
| + | * [http://openbookproject.net/thinkcs/python/english2e/ch07.html How to Think Like a Computer Scientist: Chapter 7] | ||
| − | # Create a string "The quick brown fox | + | ==Exercises== |
| + | * Questions 1-11, assume you have the following representation of the string | ||
| + | |||
| + | <code>schoolName = "Washington-Lee H.S."</code> | ||
| + | |||
| + | {|- border="1" | ||
| + | | 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: | ||
| + | #<code>schoolName[0]</code> | ||
| + | #<code>schoolName[6]</code> | ||
| + | #<code>schoolName[3:8]</code> | ||
| + | #<code>schoolName[:10]</code> | ||
| + | #<code>schoolName[15:]</code> | ||
| + | #<code>schoolName[19]</code> | ||
| + | #<code>len(schoolName)</code> | ||
| + | #<code>len("")</code> | ||
| + | #What range specifies “Lee”? | ||
| + | #If you had a variable called '''sample''', how could you return the last character, even if you didn't know its length? | ||
| + | # Create a string "The quick brown fox jumps 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 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 "q" using s and the index 4 | ||
| Line 13: | Line 80: | ||
#* Using string slicing, print out the word "brown" | #* Using string slicing, print out the word "brown" | ||
#* 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 " | + | #* Using string slicing, print out starting at the word "jumps" through the end of the string |
| − | # Use the string variable s from Question | + | # 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 | + | # 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 | + | # 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 the document via APS Canvas | ||
Latest revision as of 13:13, 18 December 2017
Contents
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
- Open a Google Doc
- Title the file: Python String Exercises
- Put your name at the top
- Complete the exercises below in this file
- Be sure to number each of your answers!!
- Submit the document via APS Canvas
References
Exercises
- Questions 1-11, assume you have the 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”?
- If you had a variable called sample, how could you return the last character, even if you didn't know its length?
- Create a string "The quick brown fox jumps 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 "jumps" 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 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
Submit:
- Submit the document via APS Canvas