''' 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 # lines below define strings a = 'abcd' b = 'a b c d' c = 'a b' d = 'a\tb' # \ followed by some character that indicates a special character in the string; # this is called an escape sequence e = 'it\'s' f = 'a\nb' # \t is a tab # \\ is a backslash # \n is a new line # \' is an apostrophe or a character quote # each escape sequence is considered one character in the string print( '##### string length and \\' ) # len() is a built-in function that tells us the length (number of characters or elements) in a sequence na = len( a ) # len(a) = 4 nb = len( b ) # len(b) = 4? but spaces are characters, so len(b) = 7 nc = len( c ) # len(c) = 4? but there is one whitespace (you'll see why) so len(c) = 3 nd = len( d ) # len(d) = 4? but(you'll see why) len(d) = 3 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' ) # concatenation means putting strings together a = 'abc' b = 'def' c = a + b # should look like 'abcdef' print( a, '+', b, '=', c ) # 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' # the 2 in this line is NOT a string, and Python can't concatenate # two different data types, so we get an error # 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 # a string is a sequence of characters print( '##### [] is the string index operator' ) s = 'abcdefghij' print( 's =', s ) print( ' 0123456789' ) # an index is a number assigned to some location in a sequence; it's like a label referring to that location # counting of indices starts from 0 and end at the length of the sequence minus 1 (len(sequence) - 1) # abcdefghij the actual string # 0123456789 the indices for each character ### 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 ] # s[i] is called subscripting or slicing, pulling out some smaller sequence (or character) in s from index i 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 ] # s[i:j] slices starting from index i and ending at index j - 1 (here it gives us indices 1, 2, and 3) s2 = s[ : 4 ] # s[:j] slices starting from index 0 and ending at index j - 1 (here it gives us 0, 1, 2, and 3) s3 = s[ 1 : ] # s[i:] slices starting from index i and ending at index len(s) - 1 (here it gives us 1, 2, 3, 4, 5, 6, 7, 8, 9) s4 = s[ : ] # s[:] slices starting from index 0 and ending at index len(s) - 1 (a copy of the string; s4 is a new string) 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 unaffected. strip() produces # a new string # strip gets rid of leading and trailing whitespace (whitespace at the very beginning and # very end) # some functions do not automatically update values; q.strip() by itself doesn't change q print( "q = '" + q + "'" ) ### pause to think what has happened print(); input( 'Enter when ready: ' ); print() s = q.strip() # q is still unaffected, but s is a # stripped version of s # q = q.strip() will change q since the = tells us we are assigning a new value to the label q # why are some functions like this: s.function() versus this function(s)? # more on the specifics of this later...but it basically amounts to whatever kind of object you're manipulating # and what kinds of functions can manipulate it 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()