CSE Activity 1.3.5 Strings



Activity 1.3.5 Strings Answer KeyIntroductionText– it's all around us. Books, web pages, email, text messages, papers for school. These are mostly text, and most of it has been digitized, represented with zeros and ones. Most programming languages have a data type called a string. A string takes care of the details for us, and we can just think of some text as a string of characters. One character might be a letter, number, or symbol, depending on the character set of the representation standard.Why do you suppose computer scientists chose the term string? Keep cats off string.ProcedureForm pairs as directed by your teacher. Meet or greet each other to practice professional skills and establish norms. Launch Canopy and open an editor window. If your teacher directs you to turn in your work with an IPython log, set the working directory for the IPython session, turn on session logging, and title your log. In []: %logstart -ort JDoeBSmith1_3_5.logIn []: # Jane Doe 1.3.5 IPython logStart a new program in the code editor by choosing File > New > Python file. Save the file as JDoe_BSmith_1_3_5.py In previous activities you learned that you can make decisions by evaluating Boolean expressions. You also learned input and output.In []: from __future__ import print_functionIn []: if raw_input('One character: ') == '!': ...: print('Wow', end='!')One character: !Wow!In addition to the native types we’ve seen so far (int, float, long, bool), another type (str) represents strings of characters. You can use the function type() to check the variable type of a variable or expression. In []: slogan = 'My school is the best'In []: type(slogan)Out[]: strIn []: sloganOut[]: 'My school is the best'Which of these types can represent the number six million? A correct answer should include all but the bool type but would still be correct by including the bool type. Data of type int, float, long, and str all could easily be used represent the number six million. If represented as a string, it would have to be converted to another type before arithmetic were performed with it. The point of the question is to point out that numbers are represented with strings – such as in text files or reading from user keyboard entry – and to give opportunity for discussion about why arithmetic nevertheless doesn't use strings. A student thinking beyond the intent of the question would also be correct in thinking that six million could also be represented by a sequence of bools. T F T T F T T T F F F T T F T T F F F F F F FString literals are enclosed in single or double quotes. The opening and closing quotes must match. One of the following two inputs will produce an error. Try this, discuss both outputs with your partner, and summarize your discussion.In []: type('tr' + "y this")In []: type('tr' + 5)In []: #7. (Discuss and explain.)You can't add a string to an int. A string is not treated as numeric data by various operators and functions.Strings are iterables. Iterables are sequences that can be counted in order, one at a time, during iteration. Strings contain a sequence of characters, one after another. The elements — including the spaces — are indexed, starting at 0. 'My school is the best'CharacterMyspacescho…tIndex #0123456…20Try this, discuss the outputs with your partner, and summarize your discussion.In []: slogan[0] Out[]: 'M'# The first characterIn []: slogan[2]# Space is the third character.In []: slogan[8]# 'l' is the ninth characterIn []: slogan[26]# The string doesn't have a 27th characterIn []: slogan[-2] # Unique to Python: index<0 counts from end # 's' is the 2nd to last character.In []: #8. (Discuss and explain.)The 0 index gave the 1st character. Python allows iterables to be sliced. To slice, use square brackets and two indices separated by a colon. Python returns the iterable from the beginning index up to but not including the ending index.In []: slogan[0:5] # Note that slogan[5] is ‘h’Out[]: 'My sc' In []: slogan[5:21]Out[]: 'hool is the best'When slicing, you can omit the starting (or ending) index if you want to start at the beginning (or end at the ending) of the string.In []: slogan[:5]Out[]: 'My sc'Try to return 'best' by slicing the variable slogan, omitting the end index.In []: # 9. SlicingIn []: (Write code to return 'best'.) slogan[-4:] or slogan[17:] Use slicing and concatenation to create your own sentence. Concatenation involves pasting together two strings, one after another. Follow the example here.In []: slogan[:13] + 'awesome!'Out[]: 'My school is awesome!'Answers vary but should look like variable[:] + 'fsd'The len() function returns the number of elements in an iterable. The index of the last element is always one less than the length of the iterable since the indices begin at 0.In []: len(slogan)Out[]: 21Explain the output of the following inputs:In []: activity = 'theater'In []: len(activity)In []: # 11a. (Discuss and explain.)There are 7 characters.In []: activity[0 : len(activity)-1]In []: # 11b. (Discuss and explain.)len(activity)-1 is 6, so the expression that was input is activity[0:6], which omits the character at index 6, which is the last and 7th character. That's why the interpreter returns 'theate' for the expression.The in keyword can be used as a Boolean condition, returning True or False:In []: 'test goo' in 'Greatest good for the greatest number!'Out[]: TrueIn []: # 12. (Discuss and explain.)This is True because 'test goo' is a substring within 'Greatest good'. (Note: the quote is a summary of John Stuart Mill's utilitarian morality.)A social media site offers a contest to write a humorous short paragraph. A constraint on the creative format: the entry must include a question, a quote, a compound sentence, and an exclamation. These would contain the characters ?, ", ,, and !, respectively. Create a function how_eligible(essay) that returns 0 to 4, equal to the number of these four characters that the essay included. As pair programmers generate ideas for how to solve this problem, strategize and then code and test iteratively.In []: how_eligible('This? "Yes." No, not really!')Out[]: 4In []: how_eligible('Really, not a compound sentence.')Out[]: 1The 1.3.5 teacherSourceFiles.zip contains an example solution and a function to autograde student submissions. ConclusionHow many characters are in this sentence? Does it matter whether Python is storing the string as one byte per character or four bytes per character? The words contain 3, 4, 10, 3, 2, 4, and 8 letters. With the 6 spaces between words and 1 character for the question mark, the sentence contains 41 characters. It is likely to be stored in 41 consecutive bytes, but this depends on how the string is represented by a particular interpreter or compiler. The details usually don't matter if your focus is at the higher level of abstraction of an algorithm that uses the string. This question asks you about something you have not learned. In fact, the question is asking about details that go beyond what you will learn in this course. However, wondering what is going on at a lower level of abstraction – and talking about it – can be a useful strategy when learning about computing. Describe what you think occurs in memory when the following code is executed.In []: a = 'one string'In []: b = 'another'In []: c = a[:3] + ' and ' + bIn []: print c[6:10]Answers will vary. What actually happens in memory? Students might not have the conception that there are different types of memory: registers that are part of the CPU, a cache that is part of the CPU, RAM, hard drive, and flash storage. The "correct" answer to this question is not important. The purpose of the question is to discover students' conceptions about memory, instructions, and variables. In fact, there is no single correct answer for all of Python, because the memory representation of strings varies from CPython to IronPython to Jython.What actually happens in memory in CPython, which we are using? To dive down the ladder of abstraction, you would look at the code for the Python interpreter, which is written in C and maintained using the Mercurial version control system: that can be abstracted for the student:The instructions from the keyboard enter RAM, are interpreted by the Python interpreter, which is machine bytecode from a C compiler. Bytecode instructions are executed by loading one instruction at a time into the CPU. These instructions create three new addresses in the namespace look-up table in RAM. The strings 'one string' and 'another' are copied from the program to the RAM. Two of the namespace addresses point to the corresponding string objects, which include an array of bytes, one character per byte. a[:3], ' and ' in the third instruction lead to two more string objects being created, and a fifth string object is created for the concatenated result. Python constructs yet a sixth string for the slice in the final command, which is created, stored, and then accessed by the print command. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download