"foo" in "buffoon" "foo" in "reginald" "foodie".startswith("foo") "foodie".endswith("foo") print "foodie".isdigit() print "4567".isdigit() print "foodie".islower() print "foodie".isupper() print "YELLING ON THE INTERNET".islower() print "YELLING ON THE INTERNET".isupper() print "Now is the winter of our discontent".find("win") print "Now is the winter of our discontent".find("lose") print "I got rhythm, I got music, I got my man, who could ask for anything more".count("I got") message = "bungalow" message[3] message[1:6] message[:3] message[2:] message[-2] shakespeare = "Now is the winter of our discontent" substr_index = shakespeare.find("win") print shakespeare[substr_index:] "ARGUMENTATION! DISAGREEMENT! STRIFE!".lower() "e.e. cummings is. not. happy about this.".upper() "dr. strangelove, or, how I learned to love the bomb".title() " got some random whitespace in some places here ".strip() "I got rhythm, I got music, I got my man, who could ask for anything more".replace("I got", "I used to have") print "include \"double quotes\" (inside of a double-quoted string)" print 'include \'single quotes\' (inside of a single-quoted string)' print "one\ttab, two\ttabs" print "new\nline" print "include an actual backslash \\ (two backslashes in the string)" input_str = "here's a zip code: 12345. 567 isn't a zip code, but 45678 is. 23456? yet another zip code." current = "" zips = [] for ch in input_str: if ch in '0123456789': current += ch else: current = "" if len(current) == 5: zips.append(current) current = "" print zips import re zips = re.findall(r"\d{5}", input_str) print zips import urllib urllib.urlretrieve("https://raw.githubusercontent.com/ledeprogram/courses/master/databases/data/enronsubjects.txt", "enronsubjects.txt") subjects = [x.strip() for x in open("enronsubjects.txt").readlines()] import re [line for line in subjects if re.search("shipping", line)] [line for line in subjects if re.search("sh.pping", line)] [line for line in subjects if re.search(r"\d:\d\d\wm", line)] print "this is\na test" print r"this is\na test" print "I love \\ backslashes!" print r"I love \ backslashes!" [line for line in subjects if re.search(r"[aeiou][aeiou][aeiou][aeiou]", line)] [line for line in subjects if re.search(r"^[Nn]ew [Yy]ork", line)] [line for line in subjects if re.search(r"\.\.\.$", line)] [line for line in subjects if re.search(r"\b[Oo]il\b", line)] [line for line in subjects if re.search(r"[A-Z]{15,}", line)] [line for line in subjects if re.search(r"[aeiou]{5}", line)] len([line for line in subjects if re.search(r"^F[Ww]d?:", line)]) [line for line in subjects if re.search(r"\b[Nn]ews\b.*!$", line)] len([line for line in subjects if re.search(r"^(?:Re|Fwd):", line)]) [line for line in subjects if re.search(r"\b(?:[Rr]ed|[Yy]ellow|[Bb]lue)\b", line)] import re print re.findall(r"\b\w{5}\b", "alpha beta gamma delta epsilon zeta eta theta") all_subjects = open("enronsubjects.txt").read() re.findall(r"\b\w+\.(?:com|net|org)", all_subjects) re.findall(r"New York \b\w+\b", all_subjects) sorted(re.findall(r"\b\d{5}\b", all_subjects)) [line for line in subjects if re.search(r"\$", line)] re.findall(r"\$[0-9.]+ ?(?:[Kk]|[Mm]|[Bb])", all_subjects) total_value = 0 dollar_amounts = re.findall(r"\$\d+ ?(?:[Kk]|[Mm]|[Bb])", all_subjects) for amount in dollar_amounts: # the last character will be 'k', 'm', or 'b'; "normalize" by making lowercase. multiplier = amount[-1].lower() # trim off the beginning $ and ending multiplier value amount = amount[1:-1] # remove any remaining whitespace amount = amount.strip() # convert to a floating-point number float_amount = float(amount) # multiply by an amount, based on what the last character was if multiplier == 'k': float_amount = float_amount * 1000 elif multiplier == 'm': float_amount = float_amount * 1000000 elif multiplier == 'b': float_amount = float_amount * 1000000000 # add to total value total_value = total_value + float_amount print total_value print int(total_value)