Skip to content

Homework 2 questions

Klementyna Kasraie edited this page Sep 21, 2017 · 1 revision

Question 3

I still cannot figure out how to replace !@ect to '999'.

There are dedicated tools for doing text processing in python. Check out the 're' package, for example, with functions like re.sub().

However, if you choose to use just base python, here is a hint for approaching this problem (this does not give you the right answer, it is meant to give you one way of thinking about the problem!)

s='abcdefg'
trans = dict(zip(s,range(1,len(s))))

Then, given a word like word="abadag" you can use a list comprehension for items in s:
".".join([str(trans[letter]) for letter in word])
NOTE: using trans.get() instead of trans[] allows you to set a default output value when the key is not found in the dictionary trans.