#String Operators #Let's do a substring fullname = 'David Lucus' #string slicing #Let's just grab the first 5 letters fname = fullname[:5] print(fname) #Let's grab the last 5 characters lname = fullname[6:] print(lname) #Let's grab the middle 5 characters randomtext = fullname[3:7] print(randomtext) #Bad fullname fullnameBad = 'David Lucus ' #Let's split on spaces #split removes the character you are searching for and makes an #array item for each step that is found. example, let's split on a #space in the variable fullnameBad #Note, this is frankly, one of hte most useful string functions available. Example, #if you wanted to parse out a text document to be word by word in an array for analysis, #in this case, just split on the spaces, or by the periods to get by sentence, or by the #\n for paragraphs, etc. arr = fullnameBad.split(' ') print(len(arr)) ssn = '555-05-5332' ssnarr = ssn.split('-') #Note the built in function list in the slides. #here we use strip to remove all leading and trailing spaces #I use concatenation, so you can see the trailing space is gone. print('Unstripped: ' + '|' + fullnameBad + '|') print('Stripped: ' + '|' + fullnameBad.strip() + '|') #Final sentence example #Note, in a real example, we would parse out all other markers like commas #unless we are trying to deal at the phrase level. paragraph = 'Hello, my name is bob. I like dogs.' #Let's get a list of sentences: sentences = paragraph.split('.') #let's iterate over the sentences: for s in sentences: print(s) #What if we wanted to iterate over words, then we need a split inside the loop that we iterate on for s in sentences: words = s.split(' ') for w in words: #you could test w if it was positive ('like, love, etc') or negative ('dislike, hate, etc') print(w)