Strings

Today we discuss one of the most important data types, strings. Even if your data is encoded as numbers, it often enters your program as strings that need to be converted. As a data scientist, you will face data in a million different formats, and most of these will enter as strings that need to be filtered, cleaned, split, and interpreted. Today we look at strings and how to manipulate them.

Reading: Chapter 5

Read Chapter 6 on strings and stop when you reach the "Format operator".

Today in Class

We will discuss and learn about:

You should be able to understand these programs.

# Input a sentence: "Hello there Mrs. Watkins how's the day?"
sent = input("Sentence: ")

# Find where Mr. or Mrs. starts
start = sent.find('Mr.')
if start == -1:
   start = sent.find('Mrs.')

# Extract the name following Mr./Mrs.
if start > -1:
   s = sent[start+4:].strip()
   space = s.find(' ')
   s = s[0:space]
   s = s.strip(',!?:;)')

print('Found the name: ' + s)

This program reads a sentence and constructs a secret word from it (all first letters of the input words).

sent = input('Secret sentence? ')
sent = sent.strip() # remove any extra spaces on ends
code = sent[0]      # first letter of first word

# While there are still spaces in the string (sentence).
while sent.find(' ') > -1:
   space = sent.find(' ')
   sent = sent[space+1:]
   code = code + sent[0]

print('SECRET: ' + code)