Difference between revisions of "Python String Exercises"
From WLCS
(→Directions) |
|||
| Line 6: | Line 6: | ||
Create a python file named "ch7_YOURLASTNAME.py". In this file, complete the following exercises: | Create a python file named "ch7_YOURLASTNAME.py". In this file, complete the following exercises: | ||
| + | == Exercises == | ||
| + | #Complete the following representation of the string <code>schoolName="Washington Lee H.S."</code> | ||
| + | {|- border="1" | ||
| + | | 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: | ||
| + | #<code>schoolName[0]</code> | ||
| + | #<code>schoolName[6]</code> | ||
| + | #<code>schoolName[3]=”X”</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”? | ||
| + | #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 | # 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 first letter in s using the brackets (e.g. s[NUM]) | ||
Revision as of 10:50, 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
Create a python file named "ch7_YOURLASTNAME.py". In this file, complete the following exercises:
Exercises
- Complete the 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:
schoolName[0]schoolName[6]schoolName[3]=”X”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 1. Create a loop that prints out every letter in the variable s
- Use the string variable s from Question 1. Create a loop that prints out every letter in s backwards starting from the end.
- Use the string variable s from Question 1. Create a loop that prints out every other letter in s starting from the beginning