# [ ] access and print the second character from planet_name: "u"
planet_name = "Jupiter"
print(planet_name[1])
u
# [ ] access and print the first character from planet_name: "J"
planet_name = "Jupiter"
print(planet_name[0])
J
# [ ] access and print the first and last characters from planet_name
planet_name = "Jupiter"
print(planet_name[0]+planet_name[-1])
Jr
# [ ] using a negative index access and print the first character from planet_name: "J"
planet_name = "Jupiter"
print(planet_name[-7])
J
# [ ] print planet_name sliced into the first 3 characters and remaining characters
planet_name = "Neptune"
print(planet_name[0:3],planet_name[3:])
Nep tune
# [ ] print 1st char and then every 3rd char of wise_words
# use string slice with a step
wise_words = 'Play it who opens'
print(wise_words[::3])
Python
# [ ] print planet_name in reverse
print(planet_name[::-1])
enutpeN
# [ ] find the number of 0's in the first half (slice) of work_tip
mid_pt=int(len(wise_words[:])/2)
print(wise_words[mid_pt:].find("o"))
2
# [ ] print a substring of code_tip set start index = 13 and end index = 25
print(wise_words[5:11])
it who
# [ ] Get user input for 1 fav_food
# [ ] iterate through letters in fav_food
# - print each letter on a new line
fav_food=input("What is your favorite food?")
for letter in fav_food:
print(letter)
p
i
z
z
a
# [ ] iterate work_tip string concatenate each letter to variable: new_string
# [ ] concatenate the letter or a "-" instead of a space " "
# tip: concatenate string example: word = word + "a"
work_tip = "Good code is commented code"
new_string=""
for letter in work_tip:
if letter==" ":
new_string+="-"
else:
new_string+=letter
print(new_string)
Good-code-is-commented-code
# [ ] Print the first 4 letters of name on new line
name = "Hiroto"
print(name[:4])
Hiro
# [ ] Print every other letter from 2nd to last letter of name
name = "Hiroto"
print(name[1::2])
ioo
# [ ] Create Mystery Name
first_name=input("Enter your first name: ")
new_name=""
for letter in first_name[::-1]:
if letter=="e":
new_name+="?"
elif letter=="t":
new_name+="?"
elif letter=="a":
new_name+="?"
else:
new_name+=letter
print(new_name)
n?d??rB
# [ ] find and display the length of the string: topic
topic = ".find() returns the length of a string"
print(len(topic))
38
# [ ] use len() to find and display the mid_pt (middle) index (+/- 1) of the string: topic
# note: index values are whole numbers
topic = "len() can take a sequence, like a string, as an argument"
mid_pt=int(len(topic)/2)
print(mid_pt)
28
# [ ] print index where first instance of the word "code" starts using .find()
work_tip = "Good code is commented code"
print(work_tip.find("code"))
5
# [ ] search for "code" in code_tip using .find()
# [ ] search substring with substring index start= 13,end = last char
# [ ] save result in varible: code_index
# [ ] display index of where "code" is found, or print "not found" if code_index == -1
work_tip = "Good code is commented code"
code_index=work_tip[13:].find("code")
search=work_tip.find("code")
if code_index==-1:
print("not found")
else:
print(code_index)
10
# [ ] find and report (print) number of w's, o's + use of word "code"
work_tip = "Good code is commented code"
print("The letter w is found",work_tip.find("w"), "times.")
print(work_tip.find("o"))
print(work_tip.find("code"))
The letter w is found -1 times.
1
5
# [ ] count times letter "i" appears in code_tip string
# [ ] find and display the index of all the letter i's in code_tip
# Remember: if .find("i") has No Match, -1 is returned
code_tip = "code a conditional decision like you would say it"
index=0
for letter in code_tip:
if letter=="i":
print(index)
index+=1
else:
index+=1
11
13
22
24
29
47
# [] create words after "G"
# sample quote "Wheresoever you go, go with all your heart" ~ Confucius (551 BC - 479 BC)
quote = input("Enter a quote: ")
word = ""
newQuote=""
for letter in quote:
if letter.isalpha():
word += letter
elif word.lower() > "g":
newQuote += word +" "
word = ""
elif word > "G":
newQuote += word +" "
word = ""
else:
word = ""
print(newQuote)
get out of