2017-02-14 HW

posted Feb 14, 2017, 6:49 AM by Samuel Konstantinovich   [ updated Feb 15, 2017, 6:32 AM ]
Goal: Strings

A complete list of string methods are here:

s='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_name
make_out_word
first_half
non_start



MORE 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!") # -1


s.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:
Comments