#Kevin Solis Gomez
#October 31, 2022
#Module 8 Section 1.4
#Today I learned to use len() .count()
# [ ] review and run example
work_tip = "save your code"
print("number of characters in string")
print(len(work_tip),"\n")
print('letter "e" occurrences')
print(work_tip.count("e"),"\n")
print("find the index of the first space")
print(work_tip.find(" "),"\n")
print('find the index of "u" searching a slice work_tip[3:9] -', work_tip[3:9])
print(work_tip.find("u",3,9),"\n")
print('find the index of "e" searching a slice work_tip[4:] -', work_tip[4:])
print(work_tip.find("e",4))
# [ ] review and run example
work_tip = "good code is commented code"
print("The sentence: \"" + work_tip + "\" has character length = ", len(work_tip) )
# [ ] review and run example
# find the middle index
work_tip = "good code is commented code"
mid_pt = int(len(work_tip)/2)
# print 1st half of sentence
print(work_tip[:mid_pt])
# print the 2nd half of sentence
print(work_tip[mid_pt:])
# [ ] review and run example
print(work_tip)
print("how many w's? ", work_tip.count("w"))
print("how many o's? ", work_tip.count("o"))
print("uses 'code', how many times? ", work_tip.count("code"))
# [ ] review and run example
print(work_tip[:mid_pt])
print("# o's in first half")
print(work_tip[:mid_pt].count("o"))
print()
print(work_tip[mid_pt:])
print("# o's in second half")
print(work_tip[mid_pt:].count("o"))
# [ ] review and run example
work_tip = "good code has meaningful variable names"
print(work_tip)
# index where first instance of "code" starts
code_here = work_tip.find("code")
print(code_here, '= starting index for "code"')
# [ ] review and run example
# set start index = 13 and end index = 33
print('search for "meaning" in the sub-string:', work_tip[13:33],"\n")
meaning_here = work_tip.find("meaning",13,33)
print('"meaning" found in work_tip[13:33] sub-string search at index', meaning_here)
# [ ] review and run example
# if .find("o") has No Match, -1 is returned
print ("work_tip:" , work_tip)
location = work_tip.find("o")
# keeps looping until location = -1 (no "o" found)
while location >= 0:
print("'o' at index =", location)
# find("o", location + 1) looks for a "o" after index the first "o" was found
location = work_tip.find("o", location + 1)
print("no more o's")
# [ ] use len() to find the midpoint of the string
# [ ] print the halves on separate lines
random_tip = "wear a hat when it rains"
print(len(random_tip))
print('The mid-point of','\"' + random_tip + '\"','is',len(random_tip)/2)
print(random_tip[:11],'\n'+random_tip[11:])
# for letters: "e" and "a" in random_tip
# [ ] print letter counts
# [ ] BONUS: print which letter is most frequent
random_tip = "wear a hat when it rains"
print(random_tip.count("e"))
print(random_tip.count("a"))
if random_tip.count('e') >= random_tip.count('a'):
print('random_tip has more e\'s than a\'s.' + str(random_tip.count('e'))+':'+str(random_tip.count('a')))
else:
print('random_tip has more a\'s than e\'s. ' + str(random_tip.count('e'))+':'+str(random_tip.count('a')))
# [ ] print long_word from the location of the first and second "t"
long_word = "juxtaposition"
print(len(long_word))
print('There are',long_word.count('t'),'t\'s in',long_word)
print('The first t is in index',long_word.find('t'))
print('The second t is in index',long_word.find('t',4,12))
quote = "they stumble who run fast"
def slice_word(word):
'''Why?'''
start = 0
space_index = word.find(' ')
while space_index != -1:
print(word[start:space_index])
start = space_index + 1
space_index = word.find(' ',start)
slice_word(quote)