COS 100: Introduction to Programming
Interim 2022
Lab 8: Working with strings
In-class practice labs will not directly impact your grade (although you'll get your standard participation point for staying on task), and will serve to help you with homework projects.Lab 8A: Wrapping long lines
- Goal: Practice with strings (and functions).
- Prompt the user for an integer for width.
- Prompt the user for a sentence (which may or may not include spaces).
- Loop through and print the sentence wrapped so every line (except the last) has width as the user requested.
- Continue to use functions properly. (Which task(s) should you leave in the main function? Which task(s) should you put in a separate function? What should you name that function? Even though there are a lot of different ways to do this, there is more or less just one preferred way to organize your code. Think through this carefully before you start coding.)
-
Sample run:
Width: 4 Sentence: Computer Science is pretty fun! Comp uter Sci ence is pret ty f un!
-
Suggested order of development:
- By now, you should be very comfortable to prompt user for an integer width, like this:
width = int(input("Width: "))
- However, do NOT prompt the user for anything, but rather, just hard-code the integer for now:
#width = int(input("Width: ")) width = 4
(Do the same thing for the sentence.) - This way, you can run your code quickly, without stopping to type input, while you develop your program.
- When you're done developing the code for wrapping text, add in the user interaction and make sure your code works when the user types other numbers besides
4
. -
If you are having a hard time getting started, click here for some hints.
- Write code to print out the first 4 letters.
- Write code to print out the next 4 letters.
- Write code to print out the next 4 letters.
- Write code to print out the next 4 letters.
- Write code to print out the next 4 letters.
- Write code to print out the next 4 letters.
- Etc.
- Do this enough to see a pattern. Now do it with a loop instead.
- By now, you should be very comfortable to prompt user for an integer width, like this:
Lab 8B: Counting letters
- Goal: Help you get started with a later homework project (yes, really). Make sure you save a copy of your work so you can use it later.
- Write a function that takes in two strings as input, the first is some text and the second is a single letter of the alphabet, and returns a count of how many times that letter shows up in the text.
-
For example, the string "Introduction to Programming" contains three 'n' characters.
So a call to
count_char("Introduction to Programming", "n")
should return the integer3
. -
Python strings are smart and know how to count:
text = "Introduction to Programming" letter = "n" print(text.count(letter))
will print 3. Do not use this built-in method right now. The point of this lab is to get some practice by writing your own version (using loops, of course).
Challenge Lab 8C: Counting substrings
- Make a copy of your 8B code (you'll want to use the original version for a later homework project) and change it as follows.
-
Instead of taking in a text and a letter, take in two strings
haystack
andneedle
. -
Count the number of times
needle
is found inhaystack
. -
If
needle
is a single character, your new function behaves exactly like the old one. - If your
haystack
is "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" andneedle
is "wood" then your function should return4
. -
How many times does the
needle
"HaHa" occur in thehaystack
"HaHaHaHaHa"? One could argue that there are 4 overlapping occurrences. Or one could argue that there are only 2 non-overlapping occurrences. Python's built-in"HaHaHaHaHa".count("HaHa")
returns 2. You should try to write your function in two ways, one that returns 2 and one that returns 4. (Do the one you think is easier first, then modify it to make the other version later.)