Class 08 – Friday February 4
“I've been absolutely terrified every moment of my life and I've never let it keep me from doing a single thing that I wanted to do.” ― Georgia O'Keefe
Look both ways
Important survey
- Please take.
What I want to accomplish today
- Refresh what happened last class meeting
- Provide experience with string manipulation.
- Reinforce that strings are immutable — there is no Python function to change an existing string.
- Expand problem solving skills and abilities.
Downloads
- Program finding.py
- Weird world of indexing in Python
- Program cat.py
- Introduces subscripting
- Program dashes.py
- Shows string operator
*
usage
- Program countability.py
- Introduces string method
count()
- Program replacement.py
- Introduces creating new strings through replacement
- Program slice_of_pie.py
- Introduces string slicing
- Program lickety_split.py
- Lets know you know what
split()
really produces
- Introduces word by word looping
- Program kinda_stringy.py
- Introduces character by character looping
- Homework prowess.py
To do list
- Check out epistle on Python
list
capabilities
- Check out string capabilities
- Review class artifacts
- Check out documentation on Python
random
capabilities
Program finding.py
-
Purpose
- Introduce string method
find()
. The method returns where the index (location) of its substring argument.
-
Program run
s: can anyone
----------
0123456789 <== indices into s
t: an
s.find( t ): 1
s.find( t, 1 ): 4
s.find( t, 4 ): -1
Program cat.py
-
Purpose
- Introduce the string brackets operator
[]
to access individual characters in a string.
-
Three program runs
Enter three indices: 0 8 9
cat
Enter three indices: 13 1 3
she
Enter three indices: 2 8 7
ram
Program dashes.py
-
Purpose
- Demonstrate string multiplication
50 * - is --------------------------------------------------
- * 50 is --------------------------------------------------
Program countability.py
- Introduces string function
count()
-
A possible program run
Enter text: any bananas
Enter substring: an
Enter index: 7
text.count( substring ): 3 # count all
text.count( substring, 7 ): 1 # count starting from index 7
Program replacement.py
-
Purpose
- Introduces string function
replace()
-
Some possible program runs
Enter text: yellow bellied sapsucker
Enter substring (s): ll
Enter substring (r): L L
text.replace( s, r ): yeL Low beL Lied sapsucker # text's s's replaced with r's
Enter text: yellow bellied sapsucker
Enter substring (s): e
Enter substring (r):
text.replace( s, r ): yllow bllid sapsuckr # text's s's replaced with r's
Program slice_of_pie.py
-
Purpose
- Introduce string slicing
-
Three program runs
Enter favorite pie: chocolate
Enter two indices i and j: 3 8
s[ i : j ]: colat
s[ i : ]: colate
s[ : j ]: chocolat
s[ : ]: chocolate
Enter favorite pie: pizza
Enter two indices i and j: 2 3
s[ i : j ]: z
s[ i : ]: zza
s[ : j ]: piz
s[ : ]: pizza
Enter favorite pie: garlic tart
Enter two indices i and j: 1 8
s[ i : j ]: arlic t
s[ i : ]: arlic tart
s[ : j ]: garlic t
s[ : ]: garlic tart
Program kinda_stringy.py
-
Purpose
- Introduce character by character looping
-
Some possible program runs
Enter text: four score
f
o
u
r
s
c
o
r
e
Enter text: step by step, inch by inch, Niagra Falls
s
t
e
p
b
y
s
t
e
p
,
i
n
c
h
b
y
i
n
c
h
N
i
a
g
r
a
F
a
l
l
s
Homework-- Problem solving — program prowess.py
-
Assignment 09
- Practices some string manipulation. The program task is to
- Prompt and get a line of text.
- Determine and print the length of the text.
- Determine and print the index of last character in the text.
- Determine and print the last character of the text.
- Prints its results.
-
Some possible program runs
Enter text: Hoos got your back
Input length: 18
Index of last input character: 17
Last character in text: k
Enter text: Enter text: And the winner is ...
Input length: 21
Index of last input character: 20
Last input character: .
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |