# Noemi Cabrera
# 2 November 2021
# In this lesson, I learned to convert between lists and strings using .split() and .join().
# Also, I learned to cast strings to lists using list() and direct multiple print outputs
# to a single line using the end= method. Using various of these methods is very useful to
# create an effective and condensed program.
# I had difficulties with the last program on Task 7 I had to code. My problem was with
# getting each of the integers to sum and get the correct answer, which is 210. To fix
# this, I created initialized the variable subtotal with 0 and added each item or digit
# casted to integer to subtotal using a for..in loop.
# [ In this code, the split() method is applied to the tip variable to create a list with its
# string. The list is stored in a variable and then its used with a for..in loop to display
# every item/character in the new list created out of the string.]
# review and run example
tip = "Notebooks can be exported as .pdf"
tip_words = tip.split()
print("STRING:", tip)
print("LIST:", tip_words, "\n")
for word in tip_words:
print(word)
# [ In this code, the split() method is applied to rhyme to create a list with its
# string. The list is stored in rhyme_words and then reversed by using the .reverse() method.
# With a for..in loop, each item in the rhyme_words list is printed.] review and run example
rhyme = "London bridge is falling down"
rhyme_words = rhyme.split()
rhyme_words.reverse()
for word in rhyme_words:
print(word)
# [The split() method is applied to rhyme to create a list with its string. The list is
# stored in rhyme_words. ] split the string(rhyme) into a list of words (rhyme_words)
# [The for..in loop iterates through each item in the list created and prints each in a
# new line.] print each word on it's own line
rhyme = 'Jack and Jill went up the hill To fetch a pail of water'
rhyme_words = rhyme.split()
for word in rhyme_words:
print(word)
# The split() method is applied to code_tip to create a list with its string. The list is
# stored in tip_list. The for..in loop iterates through each item in the list created and
# prints each one in a new line.
# [ ] split code_tip into a list and print the first and every other word
code_tip = "Python uses spaces for indentation"
tip_list = code_tip.split()
for word in tip_list:
print(word)
# [ In this code, the split() method is applied to code_tip to create a list with its string.
# However, this time split() has a dash as its argument, which means the string will split
# on every dash it has, but the dash will not appear in the new list created. The list is
# stored in tip_words and then printed.] review and run example
code_tip = "Python-uses-spaces-for-indentation"
tip_words = code_tip.split('-')
print(tip_words)
# [ In this code, the split() method is applied to code_tip to create a list with its string.
# However, this time split() has the letter 'a' as its argument, which means the string
# will split on every 'a' it has, but the 'a' will not appear in the new list created. The
# list is stored in tip_words and printed. ] review and run example - study the list print output
code_tip = "Python uses spaces for indentation"
# split on "a"
tip_words = code_tip.split('a')
print(code_tip)
print(tip_words)
# [ In this code, the split() method is applied to big_quote to create a list with its string.
# However, this time split() has a '/n' as its argument, which means the string will split
# on every new line it has. Since triple quotes are in the string, the format is kept in
# the new list created. The list is stored in quote_lines. The for..in loop iterates through
# each item in the list reversed using string slicing and prints each one in a new line.]
# review and run example
# triple quotes ''' ''' preserve formatting such as spaces and line breaks
big_quote = """Jack and Jill went up the hill
To fetch a pail of water
Jack fell down and broke his crown
And Jill came tumbling after"""
# split on line breaks (\n)
quote_lines = big_quote.split('\n')
print(quote_lines, '\n')
# print the list in reverse with index slicing
for line in quote_lines[::-1]:
print(line)
# In this code, the split() method is applied to poem to create a list with its string.
# However, this time split() has '*' as its argument, which means the string
# will split on every '*' it has, but the '*' will not appear in the new list created. The
# list is stored in phrases. The for..in loop iterates through
# each item in the list and prints each one in a new line in title case.
# [ ] split poem into a list of phrases by splitting on "*" a
# [ ] print each phrase on a new line in title case
poem = "Write code frequently*Save code frequently*Comment code frequently*Study code frequently*"
phrases = poem.split("*")
for phrase in phrases:
print(phrase.title())
# [ In this code, the .join() method is applied to tip_words to convert it into a string.
# You can add a separator for each list item in front of .join(), in this case is a space.] review and run example
tip_words = ['Notebooks', 'can', 'be', 'exported', 'as', '.pdf']
# join tip_words objects with spaces
print(" ".join(tip_words))
# [ In this code, the .join() method is applied to the letters list to convert it into a
# string. In this case, the separator (empty string) is stored in the no_space variable and
# then is used in front of .join(). The string is printed.] review and run example
no_space = ""
letters = ["P", "y", "t", "h", "o", "n"]
print(no_space.join(letters))
# [In this code, the .join() method is applied to word to iterate through "Iteration" and
# change how each letter is joined (the separator). Each character is first separated with
# a dash, then a space, and lastly ellipses. ]
# review and run example - .join() iterates through sequences
dash = "-"
space = " "
word = "Iteration"
ellipises = "..."
dash_join = dash.join(word)
print(dash_join)
print(space.join(word))
print(ellipises.join(word))
# [ In this code, the .join() method is applied to the letters list to convert it into a
# string. In this case, the separator '*' is used in front of .join(). The string is printed. ]
# .join() letters list objects with an Asterisk: "*"
letters = ["A", "s", "t", "e", "r", "i", "s", "k"]
print('*'.join(letters))
# [ This program lets the user choose the method they want to use to join words.
# Then, the word is printed with the method the user chooses.] complete Choose the separator
phrase_words = ['Jack', 'and', 'Jill', 'went', 'up', 'the', 'hill', 'To', 'fetch', 'a', 'pail', 'of', 'water']
separator = input("Enter the method you want to use to join words ("", *, -, ..., etc.) : ")
print(separator.join(phrase_words))
# [ In this code, the string 'Hello' is casted into a list by using the list() method. Each
# character becomes an item for the list. The list is stored in hello_letters and then printed. ]
# review and run example
hello_letters = list("Hello")
print(hello_letters)
# [ In this code, the string 'concatenates' is casted into a list by using the list() method. Each
# character becomes an item for the list. The list is stored in word_letters and then the
# join method is used to join each character with '~' and turn it back into a string. ]
# review and run example
# cast string to list
word_letters = list("concatenates")
# .join() concatenates the list
print('~'.join(word_letters))
# [ IN this code, the end= method is used to print the strings "Hello" & "world" in the
# same line. They are separated by a space, which is indicated by end= '' ] review and run example
print("Hello ", end = '')
print("world")
# [ In this code, the end= method was used to print the strings in separate lines by using
# "/n" in front of it. ] review and run example
# This is the default print end
print("Hello World!", end="\n")
print('still something to learn about print()')
# [ In this code, the for..in loop is used to iterate through each letter in the string
#"Concatenation". Then, using the end= method, each letter is added '*' at the end. ]
# review and run example
# end inserts any valid str character: A-z, 0-9,!,@,*,\n,\t or ''(empty string)...
for letter in "Concatenation":
print(letter, end='*')
# In this code, the 3 statements are displayed on the same line by using the end= method.
# They are joined with a dash and a space.
# [ ] use 3 print() statements to output text to one line
# [ ] separate the lines by using "- " (dash space)
print("I walked my dog in my neighborhood",end='- ')
print("and then bought ice cream",end='- ')
print("for my family",end='- ')
# In this code, the string in fact is casted into a list. Each character is an item
# of the list. The for..in loop iterates through fact. If the item in fact equals a
# space, then a blank string displays. If the item is not a space, then it's printed in
# the same line with the other items until a space appears.
# [] create a string (fact) of 20 or more characters and cast to a list (fact_letters)
# [] iterate fact, printing each char on one line, except for spaces print a new line
fact = "Hippopotamus milk is pink"
fact_letters = list(fact)
for letters in fact:
if letters == " ":
print()
else:
print(letters, end="")
# [ In this program, a string is casted to a list, which is made up of numbers. Using a for
# loop, each of the characters in the list is casted into an integer and added to the
# variable subtotal so that all the integers are added. This list is then casted to a string
# using .join(). The final equation of the sum and the answer are displayed.]
# create add the digits
digits = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20"
digits_l = digits.split(",")
subtotal = 0
for digit in digits_l:
subtotal += int(digit)
print(" + ".join(digits_l), "=",subtotal)