Section #3: Strings & File Reading

Written by Elyse Cornwall

If you'd like to code and test your solutions in PyCharm, download this starter project. We've included some Doctests for you to test your code with, but you should also add some of your own.

String Slicing

For both parts of this problem, you can test your answers quickly in the Python interpreter. In the terminal, type py or python3 depending on if you're using Windows or Mac, and then you can enter your own Python code on the fly.

elysecornwall$ python3
>>> s = 'PythonTime'
>>> s[0]
>>> y_index = s.find('y')
>>> y_index  # this prints whatever is stored in the variable y_index
>>> s[y_index]
>>> # you can continue typing python code

Part 1

How would you use slicing and/or string functions on s to receive the following results?

s = 'PythonTime'
    [0123456789] <- indices for your reference

Remember, strings in Python are 0-indexed. In addition, the slice s[1:8] is inclusive of the first index, and exclusive of the second (that is, it will get the string beginning at index 1 and up to, but not including, index 8: 'ythonTi').

Part 2

How would you use the function .find() on s to get the following results? You may use multiple lines of code.

s = 'a#xyz&TTT'

Bleeping File Reading!!

This is a file reading and string slicing problem that builds up to a program that can censor the "bad words" out of a text file.

Bleep Line

First, implement the function bleep_line(line, replacement), which takes in a string line and a string replacement and returns the line with any bad words replaced with whatever the replacement string is. We identify bad words using square brackets - [[bad word]] - and the string line will contain one or zero instances of bad words. Let's see some examples:

bleep_line('What the [[heck]] Richard??', 'BLEEP') -> returns 'What the BLEEP Richard??'

bleep_line('They call him [[Lord Voldemort]]', 'He Who Must Not Be Named') -> returns 'They call him He Who Must Not Be Named'

bleep_line('I don't need to swear to express my emotions', 'BLEEP') -> returns 'I don't need to swear to express my emotions'
You can find the empty bleep_line(line, replacement) starter code in the PyCharm project at the top of this handout. We've provided some Doctests for you.

File Bleeping

Now, we're going to write a function called file_bleeping(filename) that takes in the name of a text file filename, and prints the contents of the file with each line bleeped out. Just like in the previous part of this assignment, you can assume that each line only contains a single item to be bleeped out, or none at all. Hint, hint: use your helper function from the previous part to implement this function!

To choose the replacement word for each line, choose a random item out of the SAFE_WORDS list of strings that we've created for you. To retrieve a random word, you can do:

replacement = random.choice(SAFE_WORDS)        

If you're coding this somewhere other than the starter project, pretend a SAFE_WORDS list exists, or just choose a replacement word like 'BLEEP' to use for each line.

Once you've implemented both functions, you can test your code in the starter project by running these commands in your terminal (Windows computers use py instead of python3):
python3 small_bleep.txt
python3 large_bleep.txt

Num Slicing

Slice Num

In this problem, you'll write a function slice_num(s) that takes in a string s and returns the integer contained in that string. If the string contains an integer, it will be between two hashtags. There will either be one set of hashtags or none at all, in which case you can return 0. Here are a few examples:

slice_num('My favorite number is #2#!') -> returns 2

slice_num('Blahblah#12345#blah') -> returns 12345

slice_num('No numbers here') -> returns 0
Note that the values being returned are of type int, not string. We want to return the integer 2 and not the string '2'.

Once you've got slice_num working, write 2 Doctests! Look at the above examples for some ideas.

Sum Nums

Now, implement the function sum_nums(s1, s2) that takes in two strings and returns the sum of the integers in those strings. Make sure you return an integer, not a string. As in slice_num(s), any integers in these strings will be between two hashtags, and each string will have at most one integer, or none. Hint: this would be a great place to use the helper function you just wrote!