''' Purpose: continue string manipulation introduction ''' print() # built-in function len() returns the length of its string parameter # \ used to specify a special character: \t is tab, \n is new line, # \' is single quote, \" is double quote, \\ is backslash a = 'abcd' b = 'a b c d' c = 'a b' d = 'a\tb' e = 'it\'s' f = 'a\nb' print( '##### string length and \\' ) na = len( a ) nb = len( b ) nc = len( c ) nd = len( d ) ne = len( e ) nf = len( f ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() print( 'len(', a, ') =', na ) print( 'len(', b, ') =', nb ) print( 'len(', c, ') =', nc ) print( 'len(', d, ') =', nd ) print( 'len(', e, ') =', ne ) print( 'len(', f, ') =', nf ) print(); input( 'Enter when ready: ' ); print() # operator + when given two string operands does concatenation print( '##### concatenation' ) a = 'abc' b = 'def' c = a + b print( a, '+', b, '=', c ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() # operator + when given a string and non-string operands throws up #water = 'H' + 2 + '0' #print( water ) # built-in function str() returns a text representatiom of its # parameter print( '##### str() converts objects to a string representation' ) water = 'H' + str( 2 ) + 'O' print( "'H' + str( 2 ) + 'O' =", water ) print(); input( 'Enter when ready: ' ); print() # operator * when given a left operator string s and a right integer # integer operand n produces a string that is a repeated concatenation # of s, n times print( '##### * operator produces repeated concatenation' ) t = 'Wahoo-Wah!' * 3 print( "'Wahoo-Wah!' * 3 =", t ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() # operator [] is the string index operator -- it provides access to # a string subsequence. depending upon its operands it is all called # subscripting and slicing print( '##### [] is the string index operator' ) s = 'abcdefghij' print( 's =', s ) print( ' 0123456789' ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() print( '##### if the [] operand brackets an int, it is subscripting' ) x = s[ 0 ] y = s[ 4 ] z = s[ 9 ] print( 's[ 0 ] = ', x ) print( 's[ 4 ] = ', y ) print( 's[ 9 ] = ', z ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() print( '##### if the [] operand brackets a : expession, it is slicing' ) s1 = s[ 1 : 4 ] s2 = s[ : 4 ] s3 = s[ 1 : ] s4 = s[ : ] print( 's[ 1 : 4 ] = ', s1 ) print( 's[ : 4 ] = ', s2 ) print( 's[ 1 : ] = ', s3 ) print( 's[ : ] = ', s4 ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() # bonus -- stripping and splitting # strings can produced strip versions of themselves -- a # version without LEADING or TRAILING whitespace. Internal # whitespace is uneffected. The ability comes from a # a string's method split() # ------ print( '##### stripping' ) q = ' Look there is a Blibbering Humdinger ' q.strip() # q is uneffected. strip() produces # a new string print( "q = '" + q + "'" ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() s = q.strip() # q is still uneffected, but s is a # stripped version of s print( "q = '" + q + "'" ) print( "s = '" + s + "'" ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() # strings can produce their component words. The ability # come from a string's 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(); 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() s1 = f.split() # s1 is a 2-element list s2 = c.split() # s2 is a 3-element list print( 's1 = ', s1 ) print( 's2 = ', s2 ) print()