''' 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 # .strip() hands back a new string with # leading and trailing whitespace removed! # If you just do q.strip() alone in the code without # storing it in a variable .. that does NOTHING. # You need to store it in a new variable (or in the # original variable although we prefer you don't) so that the new # variable contains the updated string with the function applied to it. x = "314" int(x) # so int(x) is just called in the code but is it stored anywhere? # We did not change x! The function is called but it's value 314 isn't stored # anywhere so we have to do an assignment and store it somewhere. # x = "314" # x_int = int(x) now x_int holds 314 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 + "'" ) print(); input( "Enter when ready: " ); print() # strings can produce their component words. The ability comes # from the string member method split() # ------------- print( "### splitting" ) 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" ) s1 = f.split() # s1 is a 2-element list s2 = c.split() # s2 is a 3-element list print( "s1 = ", s1 ) print( "s2 = ", s2 )