3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 6.0 (clang-600.0.57)] Python Type "help", "copyright", "credits" or "license" for more information. # Strings # Another data type that represents text s = 'hello' s 'hello' s = "hello" s 'hello' msg = 'I love jazz' msg 'I love jazz' # Can add to a string using + msg + '!!!' 'I love jazz!!!' msg 'I love jazz' msg = msg + '!!!' msg 'I love jazz!!!' msg + 100 Traceback (most recent call last): Python Shell, prompt 14, line 1 builtins.TypeError: can only concatenate str (not "int") to str 100 + msg Traceback (most recent call last): Python Shell, prompt 15, line 1 builtins.TypeError: unsupported operand type(s) for +: 'int' and 'str' "cat" * 30 'catcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcat' "can't" "can't" 'can't' Traceback (most recent call last): Python Shell, prompt 18, line 1 Syntax Error: invalid syntax: , line 1, pos 6 # use double quotes when you have a single quote in the string 'can\'t' "can't" # String indexing msg 'I love jazz!!!' msg[0] 'I' # We start counting from 0 in Python # 0 is the first 'index' of the string # "The character at the first index of msg is 'I' " msg[1] ' ' msg[5] 'e' # To 'index' a string, we use square brackets [ ] # Slicing msg 'I love jazz!!!' msg[0:6] 'I love' msg[6] ' ' # slicing goes from the first index in the square brackets # up to but not including the last index after the colon msg[2:6] 'love' 'cat'[0] 'c' 'cat'[-1] 't' 'cat'[-2] 'a' 'cat'[1] 'a' msg 'I love jazz!!!' msg[7:] 'jazz!!!' # default start and stop # default start is the beginning # default stop is the end of the string msg[:4] 'I lo' another_msg = msg[:7] + 'waffles' another_msg 'I love waffles' msg[:7] 'I love ' another_msg = msg[:7] + 'waffles' + msg[-3:] another_msg 'I love waffles!!!' another_msg[::2] 'Ilv afe!!' # the 'stride' or 'step' another_msg[::3] 'Io fe!' another_msg[::-2] '!!efa vlI' len(msg) 14 msg 'I love jazz!!!' len("abc") 3 len(another_msg) 17 msg 'I love jazz!!!' 'jazz' in msg True 'cat' in msg False 'zzaj' in msg False # Only looks left to right, no backwards indexing 'zz' in msg True 'lv' in msg False # no skipping letters msg[0] + msg[5] + msg[-4] 'Iez' str(7) '7' str(7) + 8 Traceback (most recent call last): Python Shell, prompt 70, line 1 builtins.TypeError: can only concatenate str (not "int") to str '7' + 8 Traceback (most recent call last): Python Shell, prompt 71, line 1 builtins.TypeError: can only concatenate str (not "int") to str phrase = "Laughing Out Loud" phrase[0] + phrase[9] + phrase[-4] 'LOL' phrase[0] + phrase[9] + phrase[13] 'LOL' phrase = 'big orange cat' slice1 = phrase[:3] slice1 'big' slice2 = phrase[-4:] slice 2 Traceback (most recent call last): Python Shell, prompt 79, line 1 Syntax Error: invalid syntax: , line 1, pos 7 slice2 ' cat' slice3 = phrase[3:8] slice3 ' oran' lyrics = 'abc easy as 123' 'easy' in lyrics True str(len('mj')) '2' len('mj') 2 type(len('mj')) type('2') str(len('mj')) in lyrics True 'cab' in lyrics False '' in lyrics True '' '' # ^ the empty string len('') 0 '' + 'cat' 'cat' '' in 'cat' True s = Jacqueline s 'Jacqueline' # in general, the slicing operation looks like [x:y:z] # x where we start # y where we end # z is the step s[1::2] 'aqeie' # in this case, x = 1, y is default the end of the string # z = 2, so add every second character s[::-1] 'enileuqcaJ' # x is default start of string, y is default end of string, # z is -1, negative stride goes backwards s[::-2] 'eieqa' # z is negative stide, skip every other letter backwards s[-1::1] 'e' s[-1::-1] 'enileuqcaJ' s[::-1] 'enileuqcaJ' s[:4:-1] 'enile'