Class 08 – Friday February 19
I know that or now I know that, that is the question
We are humankind — What a mantra that could be — I'm human and kind
Got Covid Vaxed — The day before yesterday — Hug family soon.
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.
- Continue introduction to looping.
- Expand problem solving skills and abilities.
Downloads
- Program love_is_all_you_need.py
- Program kinda_stringy.py
- Program thats_amore.py
- Program prowess.py
- Program case_by_case.py
- Program countability.py
- Program find_the_spot.py
- Program replacement.py
- Program slice_of_pie.py
To do list
- Check out epistle on Python
list
capabilities
- Check out string capabilities
- Review class artifacts
- For next class check out documentation on Python
random
capabilities
Program love_is_all_you_need.py
- Prints the adage Love is all you need the requested number of times
-
Some possible program runs
Enter number of lines to be printed: 5
Love is all you need
Love is all you need
Love is all you need
Love is all you need
Love is all you need
Enter number of lines to be printed: 1
Love is all you need
Enter number of lines to be printed: 0
Program thats_amore.py
- Prompt user for text and print the words one per line
-
Some possible program runs
Enter text: love amore salanghae
love
amore
salangha
Enter text: upendo grá cinta Ai
upendo
grá
cinta
Ai
Program kinda_stringy.py
- Prints out the characters of a user-supplied string, character by character.
-
Some possible program runs
Enter a string: four score
f
o
u
r
s
c
o
r
e
Enter a string: and seven years
a
n
d
f
o
u
r
y
e
a
r
s
Problem solving — program prowess.py
- 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: .
Program case_by_case.py
- Introduces string case functions
capitalize()
,lower()
, andupper()
-
A possible program run
Enter text: thanks VERY mucH.
text: thanks VERY mucH.
text.lower(): thanks very much. # all lower case
text.upper(): THANKS VERY MUCH. # all upper case
text.capitalize(): Thanks very much. # initial character capitalized, rest lower case
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 find_the_spot.py
- Introduces string member function
find()
-
A possible program run
Enter text: aardvark
Enter substring: a
text.find( substring ): 0 # first occurrence
text.find( substring, 1 ): 1 # second occurrence
text.find( substring, 2 ): 5 # third occurrence
Program replacement.py
- Introduces string function
replace()
-
A possible program run
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
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |