''' Purpose: continue string manipulation introduction ''' import pause print() # strings can produced strip versions of themselves -- a # version without LEADING or TRAILING whitespace. Internal # whitespace is unaffected. The ability comes from the string # member method strip() # ------------ q = ' There is a door open to walk through ' print( 'q: \'' + q + '\'' ) ### pause to think what has happened print(); pause.until_ready(); print() print( '### mis-attempted stripping' ) q.strip() # q is unaffected. strip() always produces # a new string # strip() removes leading and trailing whitespaces from the result of the input(), which we store in the variable q here # ALWAYS hands back a NEW string; the original is NOT changed !!!!!!!!!!!! # in this case, q is still the same as before # split() also hands back a NEW thing, which is a list of things within that string that were separated by spaces print( 'q.strip()' ) print( 'q: \'' + q + '\'' ) ### pause to think what has happened print(); pause.until_ready(); print() print( '### stripping' ) s = q.strip() # q is still unaffected, but s is a # stripped version of q # we capture the result of the strip() by putting it into the variable s!! # q is still not changed # strip() without arguments will remove just the leading and trailing spaces (from q in this case) print( 's = q.strip()' ) print( 'q: \'' + q + '\'' ) print( 's: \'' + s + '\'' ) ### pause to think what has happened print(); pause.until_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 + '\'' ) ### pause to think what has happened print(); pause.until_ready(); print() fruit, price = f.split() # splits fruit into list of individual # words, which for this example is a # fruit and its cost # we know that f should have two things when it is split on the space # so we can have two things on the left side print( 'fruit, price = f.split()' ) print( fruit, 'costs', price, 'per pound' ) ### pause to think what has happened print(); pause.until_ready(); print() school, subject, number = c.split() # splits course listing into list of # individual words, which for this # example is a school, subject, and # course number print( 'school, subject, number = c.split()' ) print( 'School:', school, ' subject:', subject, ' number:', number ) ### pause to think what has happened print(); pause.until_ready(); print() # print( '### list making' ) # # list1 = f.split() # list1 is a 2-element list # list2 = c.split() # list2 is a 3-element list # # print( 'list1 = f.split()' ) # print( 'list2 = c.split()' ) # # print( 'list1: ', list1 ) # print( 'list2: ', list2 ) reply = input( 'Enter some words: ' ) words = reply.split() # cannot put different number of variables on the left side because we don't know how many words the user is going to type # words = list of words from the single string reply print( 'words =', words ) # if we enter 'UFOs are oval', then we split on space --> words = [ 'UFOs', 'are', 'oval' ] # if you only have one variable on the left side, then what you have is a list of things from the function split() # to look at individual things in a list, we can do repeat work # we use looping called a for loop # starts with keyword for # followed by a loop variable # followed by keyword in # lastly, followed by the name of the list you want to look at # really lastly, ends with a : # word is our loop variable (can be named anything) - we chose word because we are going through a list of words # could have been w or n or h (just make sure it makes sense to you) # translate: for each word in the list of words, we want to print the word for word in words: print( word ) # we indent code that you want to be repeated # tells python this belongs in the for loop # once there's no more code within this block, the for loop starts over until we finish the list # if reply = 'how old are you' --> words = ['how', 'old', 'are', 'you'] # first time, word = 'how' # second time, word = 'old' # third time, word = 'are' # fourth time, word = 'you' # since we run out of things, the loop ends for character in reply: print( character ) # reply is not a list, but it is a string that's still a sequence of characters # therefore, the loop variable represents each character in the string reply, not each word like we did above