# [ ] 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))
number of characters in string
14
letter "e" occurrences
2
find the index of the first space
4
find the index of "u" searching a slice work_tip[3:9] - e your
7
find the index of "e" searching a slice work_tip[4:] - your code
13
# [ ] 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")
work_tip: save your code
'o' at index = 6
'o' at index = 11
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"
mid_pt = int(len(random_tip)/2)
print(mid_pt)
print(random_tip[:mid_pt])
print(random_tip[mid_pt:])
12
wear a hat w
hen it rains
# 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("a"))
print(random_tip.count("e"))
if random_tip.count("a") > random_tip.count("e"):
print("The letter a appears more than e.")
else:
print("The letter e appears more than a.")
4
2
The letter a appears more than e.
# [ ] print long_word from the location of the first and second "t"
long_word = "juxtaposition"
location = long_word.find("t")
while location >= 0:
print("'t' at index =", location)
print(long_word[location:])
location = long_word.find("t", location + 1)
print("no more t's")
#print(long_word[3:])
#print(long_word[9:])
't' at index = 3
taposition
't' at index = 9
tion
no more t's
quote = "they stumble who run fast"
start = 0
spaceIndex = quote.find(" ")
while spaceIndex !=-1:
print (quote[start:spaceIndex])
start = spaceIndex+1
spaceIndex = quote.find(" ", spaceIndex+1)
they
stumble
who
run