''' Purpose: introduce how string accumulation works by building the reverse of a user-supplied string ''' # get text from user text = input( 'Enter text: ' ) print() # EXAMPLE OF STRING ACCUMULATION # RECAP: # STRING ACCUMULATION FORMAT # string_accumulator = '' # for ... in ...: # string_accumulator = string_accumulator + string we wanna add (or character) - uses concatenation # This builds up the string_accumulator! # initialize the reversal accumulator reverse = '' # string accumulator must be initialized to '' (empty string) - we don't know what we're # building up yet so we need to initialize the string to empty so that we can add characters / strings to it and # build up (accumulate) the final string output using the for loop for current_character in text : # add current character to the reversal print( 'Loop variable:', current_character ) # reverse = reverse + current_character # makes a copy of the string reverse = current_character + reverse # Add current_character to the front of the current version of reverse print( 'Reverse so far:', reverse ) # print out the string accumulator so far as we build it up padded = '' # another string accumulator for current_character in text : padded = padded + ' ' + current_character # the modification being added each time is ' ' + current_character print('Padded so far:', padded) # print the result print( "'" + reverse + "'") # final build up string padded = padded.strip() print( "'" + padded + "'") # final build up string