# StatesList.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-24 File created. # 2022-12-16 Converted to Python 3. # This program works like States.py but in this version texts # are handled mostly as lists of characters. # Note that the + operator can concatenate both lists and strings. westmost_state = list( "Hawaii" ) prairie_state = list( "Illinois" ) states_in_usa = westmost_state + [ ' ', ' ' ] + prairie_state print( "\n " + "".join( states_in_usa ), end="" ) golden_state = list( "California" ) states_in_usa = states_in_usa[ : 6 ] + [ ' ', ' ' ] + golden_state + \ states_in_usa[ 6 : ] print( "\n " + "".join( states_in_usa ), end="" ) eastmost_state = list( "Maine" ) # The parameter (argument) for the extend() method must be a list. states_in_usa.extend( list( " Virginia " ) ) states_in_usa.extend( eastmost_state ) print( "\n " + "".join( states_in_usa ), end="" ) index_of_last_state = "".join( states_in_usa ).rfind( " " ) # We'll delete characters from index_of_last_state to the end of the list del states_in_usa[ index_of_last_state : ] states_in_usa.extend( list( " Massachusetts" ) ) print( "\n " + "".join( states_in_usa ), end="" ) # To do the last operation, we'll convert the list of characters to a string. states_in_usa = "".join( states_in_usa ).replace( "Illinois", "Michigan" ) print( "\n " + states_in_usa )