Class 08 – Friday, September 09
I know that or now I know that, that is the question
Climate change is real — Science is truth, not fake news — Help save the future
Look both ways
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.
- Introduce looping.
- Expand problem solving skills and abilities.
Downloads
- 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
- Homework vowely.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 dashes.py
-
Purpose
- Demonstrate string multiplication
50 * - is --------------------------------------------------
- * 50 is --------------------------------------------------
Program escaping.py
-
Program importance
- Introduces the use of Python escape character
\
for representing special characters.
Program output
a: a b c d
b: "Air quotes" are annoying
c: Hello
Goodbye
len( a ): x
len( a ): 7
len( b ): 25
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 replacement.py
-
Purpose
- Introduce string method
replace()
that returns a variant of string based on replacing its substrings.
-
Two program runs
Enter text: hello mellow pillow
Enter substring (s): ll
Enter substring (r): L L
text.replace( s, r ): heL Lo meL Low piL Low # text’s s’s replaced with r’s
Enter text: jeepers creepers look at those peepers
Enter substring (s): eepers
Enter substring (r):
text.replace( s, r ): j cr look at those p # text’s s’s replaced with r’s
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 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
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: .
Problem solving — program vowely.py
-
Assignment 09
- Develop a program that reports on vowel usage in some user-specified text.
-
Suggestion
- When doing string analysis it is sometimes helpful to put a string in standard (canonical) form.
- Often that form is a stripped, lower case version of the string.
- Doing so here, makes the counting easier — for example, there is no need to separately count the number of a's and A's.
-
Fact
- A loop is not necessary.
-
Some possible program runs
Enter text: Are you doing okay today?
a: 3
e: 1
i: 1
o: 4
u: 1
Enter text: Subbookkeeper is one of my favorite words (and I am not being faceious).
a: 4
e: 7
i: 5
o: 8
u: 2
Enter text: Is rural juror a tongue twister?
a: 2
e: 2
i: 2
o: 2
u: 3
Enter text: tsk-tsk
a: 0
e: 0
i: 0
o: 0
u: 0