# # Strings # # Practice # # Where are we? if while for : str list str list tuple set Preliminary notes: - Based on (part of) Chapter 7 (which also discusses files). Specifically: upper(), lower(), replace(), join(), split() - Essentially - a toolbox. Python string methods (search for "String methods") https://docs.python.org/3/library/stdtypes.html Here: only some very common methods. - This does not pertain the issue of string formatting. - Use only the studied material. # Q. 1 upper() and lower() # Write a program that finds the frequency # of all words in a given book, case insensitive. # Assume the text does not include punctuation marks. book = "The man the sheep the Man the SHEEP" book = book.lower() word = "" words = [] for c in book: if c != " ": word = word + c else: words = words + [word] word = "" words = words + [word] freqs = [] for word in words: pair = [word, words.count(word)] if pair not in freqs: freqs = freqs + [pair] print(freqs) # Q. 2 - replace() # Write a program that given a string of # punctuation marks, ",.-", deletes them # all from a given sentence, save - used # as hyphen. s = "Sometime, I will meet $ road-runner. Perhaps - who knows." s = s.replace("-", '$') s = s.replace(" $ ", " - ") print(s) puncts = ",.-" for punct in puncts: for i in range(s.count(punct)): s = s.replace(punct, "") s = s.replace("$", "-") print(s) # Q. 3 - join() # Write a program that receives words from the # user, one after another, until 1 is entered. # The program prints the words separted one # from the other by a single space. word = input("Please enter a word: ") words = [] while word != "1": words = words + [word] word = input("Please enter a word: ") print(" ".join(words)) # Q. 4 - split() # Write a program that receives a list of # full names in the following patterns: # fullnames = ['Humberto L. Good', # 'Tanya M. Nelson', # 'Andrew D. Hendrix'] # The program rearranges the names such that # the surname appears first: # fullnames = ['Good Humberto L.', # 'Nelson Tanya M.', # 'Hendrix Andrew D.'] fullnames = ['Humberto L. Good', 'Tanya M. Nelson', 'Andrew D. Hendrix'] for i in range(len(fullnames)): names = fullnames[i].split() fullnames[i] = names[2] + " " + names[0] + " " + names[1] print(fullnames) # Q. 5 # Write a program that given a sentence, # put in parentehsis all words including # the letter e, case insensitive. sent = 'To be or not to bE' words = sent.split() newS = "" for word in words: if "e" in word.lower(): newS = newS + "(" + word + ") " else: newS = newS + word + " " newS = newS[:-1] print(newS + "-") # Q. 6 # (a) given a text with email addresses, write a program # that constructs a list including all # the email addresses. s = '''Yesterday I received an email from 123@abc.com. This was after I received an email from 456@def.ac.il. I hope that someday someone with the address 789@ghi.org will write to me. ''' words = s.split(' ') emails = [] for word in words: if "@" in word and "." in word: emails = emails + [word] print(emails) regex (b) Now include in the list only those email addresses preceeded by the word from. s = '''123@abc.com. This was after I received an email from 456@def.ac.il. I hope that someday someone with the address 789@ghi.org will write to from ''' words = s.split() print(words) emails = [] for i in range(len(words)): if ("@" in words[i] and "." in words[i] and words[i-1] == "from"): emails = emails + [words[i]] print(emails) myS = '''from XXX''' print(myS.split(' '))