# Noemi Cabrera
# 21 October 2021
# In this lesson, I learned to iterate a string by character using the for loop.
# The in keyword and any variable name that would contain the iterated letters of the
# loop are used with the for keyword. Each letter is printed in a different line.
# Also, you can use string slicing in the for loop to use a certain section of a string.
# I didn't have any difficulties in this lesson.
# [ In this code, the for loop is used to iterate through each letter in the string
# "cello". A variable of any name can be created to contain each of the letters, in
# this case the variable is letter. The letters are printed in a new line every time.]
# review and run example
word = "cello"
for letter in word:
print(letter)
# [ In this code, the for loop is used to iterate through each letter in the string
# "trumpet". In this case, The variable item contains each letter. ]
# review and run example
# note: the variable 'letter' changed to 'item'
word = "trumpet"
for item in word:
print(item)
# [ In this code, the for loop is used to iterate through each letter in the string
# "piano". In this case, the variable xyz contains each letter. ] review and run example
# note: variable is now 'xyz'
# using 'xyz', 'item' or 'letter' are all the same result
word = "piano"
for xyz in word:
print(xyz)
# [ In this for loop, the variable (ltr) containing each letter is tested to see of it's
# equal to "y". If it's true, then the letter is converted to upper case. If not true,
# then the letter will print as it is. This loop only runs one time.] review and run
# creates a new string (new_name) adding a letter (ltr) each loop
# Q?: how many times will the loop run?
student_name = "Skye"
new_name = ""
for ltr in student_name:
if ltr.lower() == "y":
new_name += ltr.upper()
else:
new_name += ltr
print(student_name,"to",new_name)
# [ The variable first_name gets input for a first name. ] Get user input for first_name
# [ The for loop iterates through each letter of the input. The variable character
# would contain each letter. ] iterate through letters in first_name
# - print each letter on a new line
first_name = input("Please, enter your first name:").title()
for character in first_name:
print(character)
# [ This program gathers input for a first name. If the letter "i" or "o" is found in
# the first_name variable, then that letter will be capitalized. If not, then the
# entered first name will print as it is. ]
# Create capitalize-io
first_name = input("Please, enter your first name:").title()
new_name = ''
for character in first_name:
if character.lower() == "i":
new_name += character.upper()
elif character.lower() == "o":
new_name += character.upper()
else:
new_name += character
print(new_name)
# [ In this code the for loop and string index slicing are used together. Instead of
# iterating the entire string "Skye," only the first 3 characters are iterated because
# the slice [:3] is used. ] review and run example
student_name = "Skye"
for letter in student_name[:3]:
print(letter)
# Iterate BACKWARDS
# [ Instead of iterating the entire string "Skye" backwards, only the first 3 characters
# are iterated by using [2::-1]. This indicates the slice starts at index 2 and goes
# backwards (-1). ] review and run example
student_name = "Skye"
# start at "y" (student_name[2]), iterate backwards
for letter in student_name[2::-1]:
print(letter)
# [ In this code, other_word consists of all the characters of "juxtaposition". The [:]
# method indicates the default start is at index 0 and the default end is the last
# charactre of the string. ] create & print a variable, other_word, made of every other
# letter in long_word
long_word = "juxtaposition"
other_word = long_word[:]
print(other_word)
# Mirror Color
# [The variable fav_color gets input for a favorite color ] get user input, fav_color
# [ The slice [::-1] reverses fav_color. Then, the original color is added to it to
# show both, like in a mirror. ] print fav_color backwards + fav_color
# example: "Red" prints "deRRed"
fav_color = input("Enter your favorite color: ").lower()
print(fav_color[::-1] + fav_color)