Class 8 – Friday, September 11
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
A name is needed — For our class's string mascot — Have you an idea?
Look both ways
Request
- One of our course members is in need of a notetaker. Please consider doing so.
What I want to accomplish today
- Provide experience with string manipulation.
- Reinforce that strings are immutable — there is no Python function to change an existing string.
- Continue gentle introduction to looping.
- Expand our problem solving skills and abilities.
Downloads
- Program prowess.py
- Program case_by_case.py
- Program countability.py
- Program find_the_spot.py
- Program replacement.py
- Program slice_of_pie.py
- Program kinda_stringy.py
- Program again_and_again.py
To do list
- Review class artifacts
- Check out my documentation on Python
random
capabilities
- Check out my epistle on Python
list
capabilities
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.
Two 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
Program slice_of_pie.py
- Introduces the sequence slicing operator
:
Two possible program runs
Enter favorite pie: anchovies and garlic
Enter two indices: 3 15
s: anchovies and garlic
i: 3
j: 15
s[ i ]: h
s[ j ]: a
s[ i : j ]: hovies and g
s[ i : ]: hovies and garlic
s[ : j ]: anchovies and g
s[ : ]: anchovies and garlic
Enter favorite pie: strawberry rhubarb
Enter two indices: 12 14
s: strawberry rhubarb
i: 12
j: 14
s[ i ]: h
s[ j ]: b
s[ i : j ]: hu
s[ i : ]: hubarb
s[ : j ]: strawberry rhu
s[ : ]: strawberry rhubarb
Program kinda_stringy.py
- Prints out the characters of two user-supplied strings, character by character. The first without a loop, the second with a loop.
Two possible program runs
Enter a three-letter string: abc
a
b
c
Enter a string: four score
f
o
u
r
s
c
o
r
e
Enter a three-letter string: def
d
e
f
Enter a string: and seven years
a
n
d
f
o
u
r
y
e
a
r
s
Program again_and_again.py
- Prints requested text requested number of times
Two possible program runs
Enter string to be printed: one more time
Enter number of times to be printed: 3
one more time
one more time
one more time
Enter string to be printed: I said how are you today
Enter number of times to be printed: 5
I said how are you today
I said how are you today
I said how are you today
I said how are you today
I said how are you today
Program shifty.py
- The program prompts for three strings and prints them in shifted order.
- That is, the third word is printed first, the first word is printed second, and the second word is printed last. This tyype of shifting is called a right circular shift. The printing of the words should be on a single line.
- The solution is not allowed to modify any existing statement.
Two possible program runs
Enter three strings: a b c
Before the shift w1, w2, and w3 are respectively: a b c
After the shift w1, w2, and w3 are respectively: c a b
Enter three strings: kiwi cucumber nectarine
Before the shift w1, w2, and w3 are respectively: kiwi cucumber nectarine
After the shift w1, w2, and w3 are respectively: nectarine kiwi cucumber
© 2020 Jim Cohoon | Resources from previous semesters are available. |