# Noemi Cabrera
# 25 October 2021
# In this lesson, I learned to use the len(), .count(), and .find() methods. These can
# be used in combination with loops, if/else staments, etc.
# len() returns the number of characters of a string.
# .count() returns the number of times a certain letter, word, or phrase appears in
# a string.
# .find() returns the index conut of the 1st character in a string.
# The difficulties I had were with the last Task where I had to create a program that
# printed each word of a string in a new line. To fix this, I added an else statement that
# included the last word of the string and printed the statement quote[start:space_index].
# [ In this code, the functions len(), .count, and .find() are used only using the
# variable work_tip. The lenght of characters of the string found the variable work
# tip is displayed by using len(). Using .count(), the # of times the letter e appears
# is displayed. Lastly, using the unction .find(), the index of a certain letter is
# found and displayed. String slicing is used to indicate from which indexes the
# letter has to be fonud in.] 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))
# [ In this code, the len() function is used to display the lenght or # of characters
# of the string "good code is commented."The lenght is added to strings by using a comma.]
# review and run example
work_tip = "good code is commented code"
print("The sentence: \"" + work_tip + "\" has character length = ", len(work_tip) )
# [ In this code, the lenght of the string "good code is commented code" is divided in
# half . Then, using string sicling, the 1st half o fthe string is printed and the 2nd
# half of the string is printed in separate lines. ] 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:])
# [ In this code, the function .count() is used. It counts how many times "w", "o",
# and "code" is found in the variable work_tip. The value is displayed.]
# 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"))
# [In this code, the .count() function is used along with string slicing. Here, the
# previous variables word_tip & mid_pt are used. The letter o is searched in the first
# and second half of the string "good code is commented" located in word_tip.]
# 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"))
# [ In this code, the function .find is used, which displays the index count of the 1st
# character of a string. In this case, it displays the starting index count of the
# substring "code" found in the variable work_tip.] 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"')
# [ In the .find() function, you can also set the start and end index in which you
# want to find the index count of the 1st character. In this case, the index count
# of the word meaning is searched from index 13 up to index 33. ] 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)
# [ In this code, the function .find() is used along with a while loop. WHile "o" is
# found in the word_tip variable, the index on which it appears will display until no
# more o's are found in word_tip (the .find() function returns -1 if no o's are found
# and the loop ends.).] 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")
# [ In this code, I divided the lenght of random_tip by 2 to find its midpoint, the
# variable who contains this is called mid_random. ] use len() to find the midpoint of
# the string
#[I printed 1st and 2nd half of random_tip by using string slicing] print the halves
# on separate lines
random_tip = "wear a hat when it rains"
mid_random = int(len(random_tip)/2)
print(random_tip[:mid_random])
print(random_tip[mid_random:])
# for letters: "e" and "a" in random_tip
# [ I used the function .count() to find how many times the letter e and a appear
# in random_tip. ] print letter counts
# [ I used an if/else statement to display which letter appeared the most in random_tip.]
# BONUS: print which letter is most frequent
random_tip = "wear a hat when it rains"
print("\'e\'appears",random_tip.count('e'),'times')
print("\'a\'appears",random_tip.count('a'),'times')
print()
if random_tip.count('e') > random_tip.count('a'):
print("\'e\' appears the most in random_tip")
else:
print("\'a\' appears the most in random_tip")
# [ ] print long_word from the location of the first and second "t"
long_word = "juxtaposition"
first_t = long_word.find("t",0,4)
second_t = long_word.find("t",4,-1)
print(long_word[first_t:9])
print(long_word[second_t:])
quote = "they stumble who run fast"
start = 0
space_index = quote.find(" ")
while space_index != -1:
print(quote[start:space_index])
start = space_index + 1
space_index = quote.find(" ", space_index + 1)
else:
print (quote[start::1])