# Noemi Cabrera
# 18 October 2021
# In this lesson, I learned how to access string characters with index. These can be postive or
# negative. The positive index always starts at 0 and goes from there, therefore, the string count is going to
# be minus 1. For example, a string has 5 characters, but the index count is 4. The negative indexes start with -1,
# which acesses the last character of any string no matter the length.
# I didn't have any difficulties in this lesson.
# [In this code, specific characters in the variable student_name are printed. The square brackets contain the
# index of a character in the string "Alton". The index starts at 0 and goes until 4. ]
# review and run example - note the first element is always index = 0
student_name = "Alton"
print(student_name[0], "<-- first character at index 0")
print(student_name[1])
print(student_name[2])
print(student_name[3])
print(student_name[4])
# [ In this code, the if/statement is used to display different answers depending on the value of the
# variable student_name. If the 1st character in student_name is j or a, a winner message will display.
# If the 1st character is not j or a, a message saying a match was not found will display.] review and run example
student_name = "Jin"
if student_name[0].lower() == "a":
print('Winner! Name starts with A:', student_name)
elif student_name[0].lower() == "j":
print('Winner! Name starts with J:', student_name)
else:
print('Not a match, try again tomorrow:', student_name)
# [ The string "Tobias" has 5 characters in total, not 6 because the string index count starts at 0.
# Therefore, this code displays an error message.] review and run ERROR example
# cannot index out of range
student_name = "Tobias"
print(student_name[6])
# [ The variable street_name is assigned "Riverdale" ] assign a string 5 or more letters long to the
# variable: street_name
# [ To display specific letter from the word "Riverdale", the index count of the string is used.
# It goes from 0 up until 4. ] print the 1st, 3rd and 5th characters
street_name = "Riverdale"
print(street_name[0])
print(street_name[2])
print(street_name[4])
# [ ] Create an input variable: team_name - ask that second letter = "i", "o", or "u"
# [ ] Test if team_name 2nd character = "i", "o", or "u" and print a message
# note: use if, elif and else
team_name = input( 'Enter a team name that has "i", "o", or "u" as its 2nd letter: ' )
if team_name.lower()[1]== "i":
print("You're title is correct!! Go ahead and use it for your team.")
elif team_name.lower()[1]== "o":
print("You're title is correct!! Go ahead and use it for your team.")
elif team_name.lower()[1]== "u":
print("You're title is correct!! Go ahead and use it for your team.")
else:
print("Sorry, your title is not valid.\nTry Again")
# [ In this code, the last character of the string "Joana" is accessed by writing [-1], which can be also
# accessed by writing [4]. Then, this is stored in a variable that is printed with more strings.]
# review and run example
student_name = "Joana"
# get last letter
end_letter = student_name[-1]
print(student_name,"ends with", "'" + end_letter + "'")
# [ In this code, the 2nd to last letter of Joana located in the student_name variable is accessed by writing
# [-2]. ] review and run example
# get second to last letter
second_last_letter = student_name[-2]
print(student_name,"has 2nd to last letter of", "'" + second_last_letter + "'")
# [ In this code, the third letter of Joana located in the student_name variable can be accessed in 2 ways.
# You can write [3] or [-2] to access the same letter in this case. ] review and run example
# you can get to the same letter with index counting + or -
print("for", student_name)
print("index 3 =", "'" + student_name[3] + "'")
print("index -2 =","'" + student_name[-2] + "'")
# [ The variable street_name is assigned "Yellowsnow" as its value. ] assign a string 5 or more letters long to the variable: street_name
# [ The negative indexes -3,-2, and -1 are used to access the last 3 letters of Yellowsnow. ]
# print the last 3 characters of street_name
street_name = "Yellowsnow"
print("for",street_name)
print("3rd to last letter:",street_name[-3])
print("2nd to last letter:",street_name[-2])
print("Last letter:",street_name[-1])
# [ ] create and assign string variable: first_name
first_name = "Adriana"
# [ ] print the first and last letters of name
print("for",first_name,":")
print("First letter:", first_name[0])
print("Last letter:", first_name[-1])
# [ The error in this code was that regular parentheses were used instead of using square brackets to
# access the last letter of "tennis". To fix this, I replaced the parentheses with square brackets. ]
# Review, Run, Fix the error using string index
shoe = "tennis"
# print the last letter
print(shoe[-1])