''' Purpose: continue string manipulation introduction ''' print() # strings can produced strip versions of themselves -- a # version without LEADING or TRAILING whitespace. Internal # whitespace is uneffected. The ability comes from the string # member method strip() # ------------ q = " Look there is a Blibbering Humdinger " print( "q = '" + q + "'" ) print(); input( "Enter when ready: " ); print() print( "### mis-attempted stripping" ) q.strip() # q is uneffected. strip() produces # a new string # SPLIT AND STRIP # Doesn't change the original string it was called on # Calls the function on the string and hands back a modified version # We can store the new string handed back in a new variable (below) print( "q = '" + q + "'" ) print(); input( "Enter when ready: " ); print() print( "### stripping" ) s = q.strip() # q is still uneffected, but s is a # stripped version of s (!!!) print( "q = '" + q + "'" ) print( "s = '" + s + "'" ) # Strings are immutable. Write this down! # You cannot change strings directly. # You need to store changed versions in a new variable. print(); input( "Enter when ready: " ); print() # strings can produce their component words. The ability comes # from the string member method split() # ------------- print( "### splitting" ) # .split() separates at whitespace by default so it will produce # ['bananas', $0.69'] # ['SEAS','CS','1112'] f = "bananas $0.69" c = "SEAS CS 1112" print( "f = '" + f + "'" ) print( "c = '" + c + "'" ) print(); input( "Enter when ready: " ); print() fruit, price = f.split() # splits fruit into list of individual # words, which for this example is a # fruit and its cost school, subject, number = c.split() # splits person into list of indiviudal # words, which for this example is a # name, home planet, and age print( fruit, "costs", price, "per pound" ) print( "School:", school, " subject:", subject, " number:", number ) ### pause to think what has happened print(); input( "Enter when ready: " ); print() print( "### list making" ) # Just to separate the different concepts for you # they're not real comments they're just there so you can quickly # identify sections of new string material. # A list is another type in Python. A LOT more on lists later. # All this code does is it stores the list inside of a variable. # so s1 stores the list ['bananas', '$0.69'] # f and c are still the strings they were assigned to. # f.split() DOESNT CHANGE f !!!!!!!!!!!!! # f is still a string but f.split() can just take that string, # create a list of split words, and store it in a variable. s1 = f.split() # s1 is a 2-element list s2 = c.split() # s2 is a 3-element list # Just like any other variable assignment you all have been doing, # you're just assigning the value of the function (the list it gives you) # to the variable. # So summary: f and c are strings, they're not changed, and we call the # function .split() on those strings and store them in separate lists # but they don't change the original strings - the original strings # in f and c don't become lists. Try printing f and c at the end. You'll # see they're exactly the same! # input() doesn't store things in variables but you could do something like # this. # three_words = input('Give me three words: ') # words_list = three_words.split() # w1, w2, w3 = words_list print( "s1 = ", s1 ) print( "s2 = ", s2 )