''' Purpose: demonstrate the printing of a string of characters ''' # get a string reply = input( 'Enter a string: ' ) ### print the characters one at a time # you don't know how many characters there are in the string reply --> need loop # reply is a SEQUENCE of characters (string) so we can go through the string in a for loop # in love_is_all_you_need, the SEQUENCE is a range of numbers # here the SEQUENCE is a string # after the keyword in, you can specify the sequence of your choice # range of numbers = sequence of numbers # string = sequence of characters # the type of sequence will determine what the loop variable represents # if range, then loop variable will take on numbers within the range of numbers # if string, then loop variable will take on each character within string # here, we name our loop variable ch (stands for character) for ch in reply: # 1st time: ch = first character in reply = reply[0] # 2nd time: ch = second character in reply = reply[1] # and so on until you run out of characters in reply print( ch ) print() hmmm = '' # hmmm is initialized as an empty string for ch in reply: print( 'hmmm before update:', hmmm ) hmmm = ch + hmmm # string concatenation # here we are building the reverse of the original string # we are sticking each character at the BEGINNING of the accumulator # the order in which you concatenate strings matter! print( 'hmmm after update:', hmmm ) # if reply = 'apple' # ch = 'a', hmmm = '' # --> ch + hmmm = 'a' + '' => hmmm = 'a' # ch = 'p', hmmm = 'a' # ch + hmmm = 'p' + 'a' => hmmm = 'pa' # ch = 'p', hmmm = 'pa' # ch + hmmm = 'p' + 'pa' => hmmm = 'ppa' # ... until you reach the end of reply # now you have hmmm as the reverse of reply