{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-10T14:14:41.763369+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1092,"instructions":"## Processing a String\n\n```python string-iteration\nword = \"Hello\"\n\nfor character in word:\n    print(character)\n```\n\nLists and strings are somewhat similar since they are both a sequence of things.\nStrings are sequences of characters, but lists can be a sequence of anything.\nThe key idea is that both strings and lists are sequences, which means that you can iterate over them with a For loop.\nWhen you iterate over a list, you get each element.\nWhen you iterate over a string, you get each character.\n\n## Using the split() method\n\n```python string-split\ntext = \"A multi-word string!\"\nwords = text.split()\nprint(words)\n\ntext = \"ONE\"\nwords = text.split()\nprint(words)\n\ntext = \"\"\nwords = text.split()\nprint(words)\n```\n\nOften, instead of processing a list character by character, we want to process it word by word, or by some other chunking of characters.\nTo make this easy, strings have a method named _split_.\nSplit is an awesome method because it is easy to use.\nWhen you call the method without arguments, a list of strings is returned from the string.\n\n## For loop and Split\n\n```python for-loop-split\nauthors = \"Alice Bob Carol\"\n\nfor author in authors.split():\n    print(\"By\", author)\n```\n\nAs demonstrated here, after we have split a string, it is easy to loop over each word.\nIn this example, we separate each author to print them individually.\n\n## Splitting on Characters\n\n```python split-on-characters\ndesserts = \"Apple Pie,Yellow Cake,Plum Tart\"\nprint(desserts.split(\",\"))\n\nemail = \"snakerybakery@example.com\"\nemail_pieces = email.split(\"@\")\nprint(email_pieces)\n\nword = \"Banana\"\nprint(word.split(\"nan\"))\n```\n\nIf you do not pass anything to split, then it splits on any kind of whitespace: spaces, tabs, and new lines.\nSometimes, we want to split on other characters.\nYou can pass a string as an argument to split on a different character.\nIn this example, we split on a comma (\"`,`\"), at symbol (\"`@`\"), and the string `\"nan\"`.\n\n## String Iteration in Three Ways\n\n```python string-iteration-three-ways\na_string = \"AD, BE, CF\"\n\n# By each individual character\nfor a_character in a_string:\n    print(a_character)\nprint(\"----\")\n\n# On chunks of items by a separator\nfor an_item in a_string.split(\",\"):\n    print(an_item)\nprint(\"----\")\n\n# On chunks of items by any whitespace\nfor a_chunk in a_string.split():\n    print(a_chunk)\n```\n\nJust to summarize, there are three major ways to iterate over a string.\nWithout the split method, you iterate by character.\nWith a parameter in the split method, you iterate by splitting the string on the parameter.\nAnd without a parameter in the split method, you iterate by splitting the string on whitespace.\n\n## Practical Split Example\n\n```python user-input\n# Get user input\nuser_input = input(\"Type numbers separated by commas: \")\n\n# Split into parts\nuser_values = user_input.split(',')\n\n# Process each part independently\ntotal = 0\nfor value in user_values:\n    value = int(value)\n    total += value\n\nprint(total)\n```\n\nHere is an example of the `split` pattern in action.\nThe first line takes a string from the user that is expected to be separated by a specific character (a comma).\nThen, the string is split into string values based on that character.\nFinally, each substring is converted to an actual integer (instead of a string full of digits) and added to the `total` variable.\nThe `split` method is critical, because the numbers might be more than one digit in length.\n\n## Summary\n\n- Lists and strings are both sequences, so you can index them and iterate over them the same way.\n- When you iterate over a string directly, the iteration variable will take on each character, one at a time.\n- You can use the `split` string method to turn a string into a list of strings.\n  - If you pass in no arguments, the string will be split on whitespace characters.\n  - If you pass in a string value, the string will be split on that string.\n- To iterate over chunks of a string, `split` the string and then use a `for` loop to iterate over the resulting list of strings.\n\n","ip_ranges":"","name":"7A2) String Iteration Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"Lists and Strings\",\n  \"youtube\": {\n    \"Bart\": \"xXjrxbn7Lt8\",\n    \"Amy\": \"e631o8adhC8\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_sequences_strings-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_sequences_strings-Amy.mp4\"\n  },\n  \"small_layout\": true,\n  \"summary\": \"In this lesson, we will learn more about lists. Specifically, the differences between lists and strings. Strings have some important differences from lists, but also some shared behavior.\"\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_sequences_strings_read","version":7},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1092,"assignment_version":7,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:46.244771+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:46.244771+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036912,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-4d144b56-66ea-4f0f-aa8c-2ac61186b170","user_id":2044668,"user_id__email":"","version":0},"success":true}
