2017-02-14 HW

posted Feb 14, 2017, 6:49 AM by Samuel Konstantinovich   [ updated Feb 15, 2017, 6:32 AM ]
 Goal: StringsA complete list of string methods are here:https://docs.python.org/2/library/stdtypes.html#string-methodss='abcdbcA' #Get single characters:s[0] # 'a's[1] # 'b'#slices of the string s[i,j] gives you the charactes starting at index i#up to but not including index j. This can be multiple characters! s[1:3] # 'bc'   s[2:7] # 'cdbcA's[x:x+n] gives you n letters, starting at x.#There is a function to calculate the length.len(s)  # 7 CodingBat String-1 (complete these 4 for homework if you do not finish)hello_namemake_out_wordfirst_halfnon_startMORE FEATURES OF STRINGS#String methods are the functions attached to the string:s.find('c') # 2 s.find('c',3) # 5 s.find('bc') # 1 s.find("fish!") # -1s.count('bc') # 2 s.count('BC') # 0 STOP! This is where we get to write something that is built in so you don't think that the methods are magical constructs created by elves in a software development fairytale.def findLetter(string,letter):  #return the index the single letter is located  #or -1 when the letter is not inside the string def countLetter(string,letter):  #return how many times the single letter is located  #in the string, 0 means it isn't there.(complete these 2 for homework if you do not finish)Both of these can be found here: http://codingbat.com/home/konstans@stuy.edu/all s.replace('bc','PQ-') # 'aPQ-dPQ-A' s.isalpha() # True '123'.isdigit() # True '123.45'.isdigit() # False s.endswith('cA') # True “ ab “.strip() # 'ab'  "abcddda".strip("ad") # 'bc' HW: I consolidated all of the codingbat assignments onto this page:http://codingbat.com/home/konstans@stuy.edu/all