{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2024-08-30T15:38:21.572933+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":954,"instructions":"## What are the operations?\n\n* Addition\n* Comparisons\n* Membership\n* Slicing\n  * Subscripting\n  * Indexing\n\nStrings have several operators that can be used on them, just like integers or Booleans.\nIn fact, they have some special operators we haven't seen before.\nWe will learn about five kinds of operations that are valid on strings in Python.\nWe have already seen related ideas in math for the addition and comparison operators.\nNow, we will also learn about the membership operator and slicing (which is divided into subscripting and indexing operations,) which are brand new.\n\n## Addition\n\n```python example-adding\nprint(\"Hello \" + \"World\")\n# \"Hello World\"\n```\n\nLike numbers, you can add two strings together.\nThis puts them side by side in a single new string.\nThis is sometimes called \"Concatenation.\"\nWhen adding two strings together you will need to add spaces within the quotation marks; Python will not automatically space the content you are bringing together.\nNotice in the example above, a space was added after Hello, but could also have been added before World instead.\n\n## Comparing Strings\n\n```python example-comparison\n# String equality\nprint(\"Dog\" == \"Dog\")\n# String inequality\nprint(\"Dog\" != \"Cat\")\n# String ordering\nprint(\"Aardvark\" < \"Zoo\")\nprint(\"Orange\" <= \"Apple\")\n```\n\nYou can test if two strings are equal (`==`), not equal (`!=`), or even compare them using less than (`<`) and greater than (`>`) operators.\nThis determines which string comes first in the alphabet.\nStrings that start with \"A\" come before strings that start with \"B,\" and strings that start with \"B\" come before strings that start with \"C.\"\n\n## Membership in Strings\n\n```python example-membership\n# String membership\nprint(\"house\" in \"boathouse\")\nprint(\"cow\" in \"cowabunga\")\nprint(\"y\" not in \"axes\")\nprint(\"xe\" not in \"axes\")\n```\n\nThere's another test you can check with strings: using the `in` operator.\nThis simply checks whether the first string appears in the second.\nYou can also use the `not in` operator to test the opposite.\n\n## Membership Rules\n\n```python membership-rules\n# All strings contain the empty string\nprint(\"\" in \"all strings contain the empty string\")\n\n# Spaces are characters too\nprint(\" \" in \"this string contains a space character\")\nprint(\" \" not in \"no_spaces_here\")\n```\n\nYou might find some of the rules for string membership a little surprising.\nRemember that symbols, whitespace characters (like the space `\" \"`), and digits are all valid characters to test for.\nThe empty string is also a valid string to test and is contained in every string.\n\n## Variables in Membership Tests\n\n```python variable-membership\n# You can use variables containing string values too\nword = \"apple\"\nprint(word in \"applesauce\")\nprint(\"p\" in word)\nprint(\"w\" not in word)\n```\n\nKeep in mind that you usually will not test if a string literal is inside of another string literal.\nInstead, you will usually use a variable holding a string value.\nDo not confuse the variable's name with the value itself!\n\n## Slicing\n\n```python example-subscripting\nmessage = \"Hello world\"\nprint(message[0])\n```\n\nSlicing is one of the more powerful and more complex features of strings.\nWhen we slice a string, we extract one or more characters from the string.\nIf we only want to get one single character from a string, we call this _indexing_ a string.\n\n## Indexing Syntax\n\n```python example-second-subscripting\nperson_name = \"Grace Hopper\"\nprint(person_name[2])\n```\n\nWe can index a string value in a variable by using square brackets.\nNotice the key components: On the left is the name of the variable or the string literal value.\nNext, we have an opening square bracket.\nThen, we have a number, which is called the index.\nFinally, we end with a closing square bracket.\n\n## Starting at 0\n\n![The string \"Hello World!\" is written with numbers below each character. The first character \"H\" has a zero below it, the second character \"e\" has a 1 below it, and so on.](intro_string_ops_indices.png)\n\nYou may be wondering why the first example printed `\"H\"` and the second example printed `\"a\"`.\nThe number inside of the brackets is an \"index\" and numbers the position of each character in the string.\nHere's a weird thing: Computers start counting at 0, not 1.\nSo, if you want the first character from the string, you need to write 0 instead of 1.\nThere are mathematical reasons why this ends up being more convenient, and it is common in most programming languages, so you should get used to it.\nNotice that when we are counting the indexes, this includes space characters \u2014 they are a part of the string and do take up space!\n\n## Subscripting Multiple Characters\n\n```python example-multiple-subscripts\nmessage = \"Hello world\"\nprint(message[2:5])\n```\n\nIt wouldn't be too useful to only grab out one character at a time.\nSo, you can grab out more than one by using the _subscript_ syntax: putting a pair of numbers separated by a colon inside the square brackets.\nThe first number represents the starting position of the subscript, and the second number indicates the ending position of the subscript.\nSo, this example prints all of the characters after the second character up to (and including) the fifth character (`\"llo\"`).\n\n## Negative Subscript Indices\n\n```python example-negative\nword = \"hamster\"\n\nprint(word[-1])\nprint(word[1:-1])\nprint(word[-3:])\nprint(word[:3])\n```\n\nIf you use negative numbers in subscripts, you can work from the back of the list.\nIf you use -1, then you get the last character.\nYou can combine positive and negative in a range.\nTo go from the start or the end, simply leave the number missing.\nIn the example shown here, the first line indexes the last character (`\"r\"`).\nThe second line subscripts all the characters after the first up to but not including the last character (`\"amste\"`).\nThe third line subscripts the last three characters at the end of the string (`\"ter\"`).\nThe fourth line subscripts the first three characters at the start of the string (`\"ham\"`).\n\n## Figuring out Subscripts\n\n![A diagram showing how to determine subscripts more easily](intro_strings_tip.png)\n\nFiguring out subscripts can be quite tricky.\nIf you try to stare and count the indexes by hand, you are almost certainly going to make a mistake.\nInstead, take out a piece of paper and draw boxes around each character of the string.\nThen, number the boxes.\nPut indexes directly BELOW the boxes, and subscripts directly BETWEEN the boxes.\nYou can then quickly check what character a given index corresponds to, or what range of characters are sandwiched between two subscripts.\n\n## Summary\n\n- Python has the following string operations:\n  - Addition (`+`) to join two strings together into a new string.\n  - Comparison (`<`, `>`, `<=`, `>=`) to test the alphabetical order of two strings.\n - Membership test (`in`) to test if one string is exactly contained inside of another.\n - Indexing to extract a single character from a specific position in a string.\n - Subscripting to extract a sequence of characters from a specific range in a string.\n- Subscripting and indexing are both forms of string slicing, which extracts characters from a string.\n- Indexes start at 0 in Python.\n- Subscripts come in pairs of start and ending positions in the string.\n- Negative indices can be used to access elements starting from the end of the list (with -1 being the last character).\n- Negative subscripts can be used to select elements from the back of the string.\n- Missing subscripts can be used to represent the start and end of the string.\n- When trying to evaluate string slices, write the string on paper with each character in a box.\n  - For indexing, put numbers directly below each box.\n  - For subscripting, put numbers between each box .","ip_ranges":"","name":"1B7) String Operations 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\": \"String Operations\",\n  \"slides\": \"bakery_intro_string_ops.pdf\",\n  \"youtube\": {\n    \"Bart\": \"dzyqo5ljGtY\",\n    \"Amy\": \"K8KWatL_DmM\"\n  },\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_intro_string_ops-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_intro_string_ops-Amy.mp4\"\n  },\n  \"summary\": \"This lesson teaches basic operations on Strings. We'll learn how to manipulate and extract information about those strings.\",\n  \"small_layout\": true\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_intro_string_ops_read","version":12},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":954,"assignment_version":12,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T23:50:36.535576+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T23:50:36.535576+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2037096,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-d83bec73-42ab-41b3-a339-746834ad43ac","user_id":2044772,"user_id__email":"","version":0},"success":true}
