''' Purpose: continue string manipulation introduction ''' # Time to learn about strings! # A string can be within "" or ' ' (double brackets or single brackets # "Cohoon" and 'Cohoon" are both a string of the name Cohoon! 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 \\' ) # So how do we find the length of string? # We use len(), which takes in a string (the literal string or a variable that stores the string like we're passing in here) and it returns # the length of the string (a number). 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() # Take a look at the string class information in the information sheet! # operator + when given two string operands does concatenation # What is concatenation? It basically glues two strings together! 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 # We can make a number or a float into a string using str() which will return the string of that integer or float! # str( 2 ) will return '2' ! print( '##### str() converts objects to a string representation' ) # Can you just concatenate a number (that's not a string) and a string? NOOOOOOOOOOOOO. # If we wanna glue together a number and a string, we want to make it the string version of that number first! # String concatenation only works with strings using the "+" operator. # That's why in this example below, we convert 2 to a string using str( 2 ) so we can concatenate strings together to create 'H2O' 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' ) # So we know about string concatenation. What does this do? It produces repeated concatenation meaning that it will produce this string # 3 times glued to one another so t = "Wahoo-Wah!Wahoo-Wah!Wahoo-Wah!" 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' ) # Remember this! Indexing strings is very useful when you want substrings (cutting strings to # get a chunk of the string you want using the indexes meaning the numeric places of the characters within the string starting with 0 and # going to the end of length of the string - 1. # So the word "mom" has a length of three, m is at index 0, o is at index 1, and m is at index 2 # Notice how indices are 0 to len ('mom' - 1 ? Length is three and the indexes are 0-2. 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' ) # Subscripting using an index gives us the character at that index within the string # for a single index value 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' ) # Slicing is getting a chunk of the string. Look at the info sheet for details! # This is taking the string and getting characters at those indexes from i to j - 1 # a b c d e f g h # 0 1 2 3 4 5 6 7 # slicing s[ 1:4 ] is starting and including index 1 ('b') all the way up to BUT not including index 4 'e' so the string is 'bcd' # Think in terms of intervals if you're a math person it's like [i, j-1) # If you don't provide i in a bracket slicing it starts at the first character [ : 4 ] # If you don't provide j (the second slot after the colon) in the brackets when slicing, it goes up to the last character [ 1 : ] # [ : ] just gets the whole string # Makes sense? 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' ) # What does .strip() do? By default, it clears whitespace before the after the string. It does not get rid of whitespace in between! # It only gets rid of whitespace for a single string at the beginning and ends. 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' ) # So we've used splitting before right? It takes a string and breaks it up into elements to put into a list. # So if I have a string s = 'I like chocolate' and i code list_words = s.split(), list_words = ['i', 'like', 'chocolate'] # So it broke it up into words in a list separated by the spaces in the string! # We can assign values (multiple assignment) by f = 'bananas $0.69' c = 'SEAS CS 1112' print( 'f = \'' + f + '\'' ) print( 'c = \'' + c + '\'' ) # So in the next few lines when we use fruit, price = f.split() this will take string f and split it into a list [ 'bananas', '$0.69' ] # So in the assignment fruit is assigned the string 'bananas' and price is assigned '$0.69' # This same .split() and multiple assignment logic applies to the next examples where c.split() creates a list of the words separated at spaces. # So basically # school, subject, number = [ 'SEAS', 'CS' , '1112' ] and each variable is assigned its own respective value in the list! ### 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()