# Zhi Yuan "Oscar" Lin
# 21 October 2021
# In this lesson, I learned more about the string methods "len()", ".count()", and ".find()" through completing
# examples and tasks. I also learned why being a programmer is difficult since there are many rules a programmer
# needs to remember.
# There were no major problems or questions throughout this lesson.
# [ ] review and run example
# The variable "work_tip" is assigned to the string "save your code".
work_tip = "save your code"
# This function displays the string "number of characters in string" in the output.
print("number of characters in string")
# This function displays the given value when the string method "len()" determines the length of the value
# assigned to the work_tip variable in the output. Additionally, a blank line will be displayed on the next
# line of the output due to the escape character "\n".
print(len(work_tip),"\n")
# This function displays the string 'letter "e" occurrences' in the output.
print('letter "e" occurrences')
# This function displays the given value when the string method ".count()" determines how many times the character
# "e" occurred in the value assigned to the work_tip variable in the output. Additionally, a blank line will be
# displayed on the next line of the output due to the escape character "\n".
print(work_tip.count("e"),"\n")
# This function displays the string "find the index of the first space" in the output.
print("find the index of the first space")
# This function displays the given value when the string method ".find()" determines the index position of the character " "
# in the value assigned to the work_tip variable in the output.
# Additionally, a blank line will be displayed on the next line of the output due to the escape character "\n".
print(work_tip.find(" "),"\n")
# This function displays the string 'find the index of "u" searching a slice work_tip[3:9] -' and the 4th to the
# 9th character of the value assigned to the work_tip variable in the output.
print('find the index of "u" searching a slice work_tip[3:9] -', work_tip[3:9])
# This function displays the given value when the string method ".find()" determines the index position of the character
# "u" from the 4th to the 9th character of the value assigned to the work_tip variable in the output. Additionally,
# a blank line will be displayed on the next line of the output due to the escape character "\n".
print(work_tip.find("u",3,9),"\n")
# This function displays the string 'find the index of "e" searching a slice work_tip[4:] -' and the 5th to the last
# character of the value assigned to the work_tip variable in the output.
print('find the index of "e" searching a slice work_tip[4:] -', work_tip[4:])
# This function displays the given value when the string method ".find()" determines the index position of the character
# "e" from the 5th to the last character of the value assigned to the work_tip variable in the output.
print(work_tip.find("e",4))
# [ ] review and run example
# The variable "work_tip" is assigned to the string "good code is commented code".
work_tip = "good code is commented code"
# This function displays the concatenated string formed from the string 'The sentence "', the value assigned to the
# work_tip variable, the string '"has character length = "', and the value given when the string method "len()"
# determines the length of the value assigned to the work_tip variable, in the output. Additionally, the double quotes
# shown in the output are due to the escape character '\"'.
print("The sentence: \"" + work_tip + "\" has character length = ", len(work_tip) )
# [ ] review and run example
# find the middle index
# The variable "work_tip" is assigned to the string "good code is commented code".
work_tip = "good code is commented code"
# The variable "mid_pt" is assigned to the value of the given value when the string method "len()" determines
# the length of the value assigned to the work_tip variable being divided by the integer "2", and converted into an
# integer due them being in the parentheses of the int() function.
mid_pt = int(len(work_tip)/2)
# print 1st half of sentence
# This function displays the first half, 1st to 13th, of the characters of the value assigned to the work_tip
# variable in the output.
print(work_tip[:mid_pt])
# print the 2nd half of sentence
# This function displays the second half, 14th to 27th, of the characters of the value assigned to the work_tip variable
# in the output.
print(work_tip[mid_pt:])
# [ ] review and run example
# This function displays the value assigned to the work_tip variable in the output.
print(work_tip)
# This function displays string "how many w's? " and the given value when the string method ".count()" determines how
# many times the character "w" occurred in the value assigned to the work_tip variable, in the output.
print("how many w's? ", work_tip.count("w"))
# This function displays string "how many o's? " and the given value when the string method ".count()" determines how
# many times the character "o" occurred in the value assigned to the work_tip variable, in the output.
print("how many o's? ", work_tip.count("o"))
# This function displays string "uses 'code', how many times? " and the given value when the string method
# ".count()" determines how many times the sub-string "code" occurred in the value assigned to the
# work_tip variable, in the output.
print("uses 'code', how many times? ", work_tip.count("code"))
# [ ] review and run example
# This function displays the first half, 1st to 13th, of the characters of the value assigned to the work_tip
# variable in the output.
print(work_tip[:mid_pt])
# This function displays the string "# o's in first half" in the output.
print("# o's in first half")
# This function displays the given value when the string method ".count()" determines how many times the character
# "o" occured from the first half, 1st to 13th, of the characters of the value assigned to the work_tip variable, in
# the output.
print(work_tip[:mid_pt].count("o"))
# This function displays a blank line in the output due to the print() function not having an argument.
print()
# This function displays the second half, 14th to 27th, of the characters of the value assigned to the work_tip variable
# in the output.
print(work_tip[mid_pt:])
# This function displays the string "# o's in second half" in the output
print("# o's in second half")
# This function displays the given value when the string method ".count()" determines how many times the character
# "o" occured from the second half, 14th to 27th, of the characters of the value assigned to the work_tip variable,
# in the output.
print(work_tip[mid_pt:].count("o"))
# [ ] review and run example
# The variable "work_tip" is assigned to the string "good code has meaningful variable names".
work_tip = "good code has meaningful variable names"
# This function displays the value assigned to the work_tip variable in the output.
print(work_tip)
# index where first instance of "code" starts
# The variable "code_here" is assigned to the given value when the string method ".find()" determines the index position
# of the start of the sub-string "code" in the value assigned to the work_tip variable.
code_here = work_tip.find("code")
# This function displays the value assigned to the code_here variable and the string '= starting index for "code"' in
# the output.
print(code_here, '= starting index for "code"')
# [ ] review and run example
# set start index = 13 and end index = 33
# This function displays the string 'search for "meaning" in the sub-string:' and the 14th to 33th character of the value
# assigned to the work_tip variable. Additionally, a blank line will be displayed on the next line of the output due to
# the escape character "\n".
print('search for "meaning" in the sub-string:', work_tip[13:33],"\n")
# The variable "meaning_here" is assigned to the given when the string method ".find()" determines the index position of
# the sub-string "meaning" from the 14th to 33th character of the value assigned to the work_tip variable.
meaning_here = work_tip.find("meaning",13,33)
# This function displays the string '"meaning" found in work_tip[13:33] sub-string search at index' and the value assigned
# to the meaning_here variable.
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
# This function displays the string "work_tip:" and the value assigned to the work_tip variable in the output.
print ("work_tip:" , work_tip)
# The variable "location" is assigned to the given value when the string method ".find()" determines the index position
# of the character "o" in the value assigned to the work_tip variable.
location = work_tip.find("o")
# keeps looping until location = -1 (no "o" found)
# In this case, the while loop is follow by statement "location >= 0". Hence, as long that is true, the codes that
# are indented and follow the while loop will be part of the loop. Thus, the loop starts with string "'o' at index ="
# and the value assigned to the location variable, that are within the print() function, being display in the output.
# Subsequently, the location variable will be reassigned to the next index position of the character "o" after the
# first "o" was found. This loop will continue until location >= is false.
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)
# This function will display the string "no more o's" in the output.
print("no more o's")
# [ ] use len() to find the midpoint of the string
# [ ] print the halves on separate lines
# The variable "random_tip" is assigned to the string "wear a hat when it rains".
random_tip = "wear a hat when it rains"
# The variable "mid" is assigned to the value of the given value when the string method "len()" determines
# the length of the value assigned to the random_tip variable being divided by the integer "2", and converted into an
# integer due them being in the parentheses of the int() function.
mid = int(len(random_tip)/2)
# This function displays the first half, 1st to 12th, of the characters in the value assigned to the random_tip
# variable in the output.
print(random_tip[:mid])
# This function displays the second half, 13th to 24th, of the characters in the value assigned to the random_tip
# variable in the output.
print(random_tip[mid:])
# for letters: "e" and "a" in random_tip
# [ ] print letter counts
# [ ] BONUS: print which letter is most frequent
# The variable "random_tip" is assigned to the string "wear a hat when it rains".
random_tip = "wear a hat when it rains"
# This function displays the given value when the string method ".count()" determines how many times the character
# "e" occurred in the value assigned to the random_tip variable in the output.
print(random_tip.count("e"))
# This function displays the given value when the string method ".count()" determines how many times the character
# "a" occurred in the value assigned to the random_tip variable.
print(random_tip.count("a"))
# This IF statement if the given value when the given value from the string method ".count()" that determines how
# many times the character "e" occurred in the value assigned to the random_tip variable is greater than the given
# value from the string method ".count()" determines how many times the character "a" occurred in the value assigned
# to the random_tip variable. If true, the string 'The letter "e" is the most frequent', that is within the parentheses
# of the print() function, will be display in the output. If false, the program moves on to the else statement with
# the string 'The letter "a" is the most frequent', that is within the parentheses of the print() function, being display
# in the output. This loop will
if random_tip.count("e") > random_tip.count("a"):
print('The letter "e" is the most frequent')
else:
print('The letter "a" is the most frequent')
# [ ] print long_word from the location of the first and second "t"
# The variable "long_word" is assigned to the string "juxtaposition".
long_word = "juxtaposition"
# The variable "location" is assigned to the given value when the string method ".find()" determines the index position
# of the character "t" in the value assigned to the long_word variable.
location = long_word.find("t")
# In this case, the while loop is follow by statement "location >= 0". Hence, as long that is true, the codes that
# are indented and follow the while loop will be part of the loop. Thus, the loop starts with string "'t' at index ="
# and the value assigned to the location variable, that are within the parentheses of the print() function, being display
# in the output. Subsequently, the location variable will be reassigned to the next index position of the character
# "t" after the first "t" was found. This loop will continue until location >= is false.
while location >= 0:
print("'t' at index =", location)
location = long_word.find("t", location + 1)
# The variable "quote" is assigned to the string "they stumble who run fast".
quote = "they stumble who run fast"
# The variable "start" is assigned to the integer "0".
start = 0
# The variable "space_index" is assigned to the given value when the string method ".find()" determines the index
# position of the character " ".
space_index = quote.find(" ")
# In this case, the while loop is follow by statement "space_index != -1". Hence, as long that is true, the codes that
# are indented and follow the while loop will be part of the loop. Thus, the loop starts with displaying the substring
# that is between the index position assigned to the start variable and index position assigned to the space_index variable
# in the value assigned to the quote variable, that is between the parentheses of the print() function, being display in
# the output. Subsequently,the start variable will be reassigned to the value assigned to the space_index variable
# incremented by the integer "1". Additionally, the space_index variable will be reassigned to the give value from the
# string method ".find()" that determines the index position for the character " " that is between the index position
# assigned to the start variable and the last character. This loop will continue until space_index != -1 is false.
while space_index != -1:
print(quote[start:space_index])
start = (space_index + 1)
space_index = quote.find(" ", start)
# This function displays the substring from the index position assigned to the start variable to the last character, in
# the output.
print(quote[start:])