def substring_example(): name = "Adriane Joy Huber" print "My name is", name # Select a subset of a string # First parameter is where the substring begins. # Second paramter is first character not included in substring. # Note character index starts with 0. middle_name = name[8:11] print "My middle name is", middle_name print def split_string_example(): address = "272 Computer Science, Fort Collins, CO, 80523-1873" address_elements = address.split(", ") print "My building address is", address_elements[0] print "My city is", address_elements[1] print "My state is", address_elements[2] print "My zip code is", address_elements[3] print def search_string_example(): # Search for a pattern at the beginning of a string phone = "970-491-4191" if phone.startswith("970"): print "I have a local phone number" if not phone.startswith("970"): print "I do not have a local phone number" print # Search for a pattern anywhere in a string if "491" in phone: print "I found 491 in,", phone else: print "I did not find 491 in,", phone # Search for a pattern at the end of a string if phone.endswith("4191"): print "I found 4191 at the end of the string,", phone else: print "I did not find 4191 at the end of the string,", phone print # Search for word and replace with a different word name = "Adriane Joy Huber" print "My formal name is", name nickname = name.replace("Adriane Joy", "D.J.") print "My nickname is", nickname print def string_case_example(): city = "indianapolis" print "Capitalize first letter of", city, ":", city.capitalize() print "All upper case of", city, ":", city.upper() print "All lower case of", city, ":", city.lower() print "First letter of each word capitalized:", city.title() substring_example() split_string_example() search_string_example() string_case_example()