''' 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 = ' There is a door open to walk through ' print( 'q: \'' + q + '\'' ) print(); input( 'Enter when ready: ' ); print() print( '### mis-attempted stripping' ) # New function! .strip() is a function for strings! q.strip() # q is uneffected. strip() always produces # a new string # q.strip() applies the strip() function to string q # The strip() function hands back a new string that has no leading and trailing whitespace. print( 'q.strip():' , q.strip() ) print( 'q: \'' + q + '\'' ) # q did not change! q.strip() does not change q!! # The function hands back a NEW string with no leading and trailing whitespace. # When you call strip() on a string, it does not change the original string. # Strings are IMMUTABLE (they cannot be changed!) - we can call functions on strings and they # hand back NEW strings that are ALTERED versions of the original string (like a copy!) # We usually do variable = q.strip() or something like that (store the stripped version in a variable) print(); input( 'Enter when ready: ' ); print() print( '### stripping' ) s = q.strip() # q is still uneffected, but s is a # stripped version of s print( 's = q.strip()' ) print( 'q: \'' + q + '\'' ) # q is unchanged print( 's: \'' + s + '\'' ) # s is a copy of q which the function applied to q (no leading / trailing whitespace) # Leading and Trailing Whitespace: Whitespace at the beginning / end of a string (doesn't get rid of space # in the middle / in between words in a string if we just call strip() with no arguments) # strip() by default removes leading/trailing whitespace # It CAN take in an optional argument in the parentheses to strip (get rid of) what you want to get # rid of in the string. # The original string is unchanged! # The stripped version is stored in another variable! 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 print( 'fruit, price = f.split()' ) print( fruit, 'costs', price, 'per pound' ) print(); input( 'Enter when ready: ' ); print() school, subject, number = c.split() # splits coutse listing into list of # individual words, which for this # example is a school, subject, and # course number # split() is another string function we touched on last class! It returns a list of words split # based on whitespace in the original string. print( 'school, subject, number = c.split()' ) print( 'School:', school, ' subject:', subject, ' number:', number ) ### pause to think what has happened print(); input( 'Enter when 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 ) # Assigning Variables to .split(): # list1 = f.split() -> STORES THE LIST OF WORDS / SPLIT STRINGS IN THE VARIABLE # fruit, price = f.split() -> ASSIGNS THE FIRST ITEM IN THE LIST TO FRUIT AND THE SECOND ITEM IN THE LIST # f is 'bananas $0.69' # f.split -> ['banana', '$0.69'] # A list in Python is denoted using brackets with items separated with commas [...,...,...] # More on lists later!