78 2 more about substrings we can return a single

This preview shows 2 out of 5 pages.

7.8 2
Image of page 2

Subscribe to view the full document.

More about substrings We can return a single character: >> s = "abcde" >> s [4] ’e’ We can also wind up with the empty string by having the right margin of a substring be at or before the left margin: >> s [4:2] ’’ ’’ is the empty string. 7.9 More about substrings We can also leave either the beginning or the end of the range empty for a substring. >> s = ’abcde’ >> s [2:] ’cde’ >> s [:2] ’ab’ The form q [ a : b ] in Python is called the slice notation. We’ll use it for lists as well. 7.10 Immutability of strings in Python You might try to change the value of a string at a particular position. For example, if you wanted to change string s at index 4, you might be tempted to say: s [4] = ’t’ This does not work, since strings in Python are immutable : you cannot modify an existing string. You can give s a new value, but you cannot mutate the string which it is pointing to. 7.11 String length To compute the length of a string my_str , we simply say: len ( my_str ) The contract can be written as: len : str int 7.12 2 Methods in Python Methods in Python Many objects in Python have things that can be done to them based on their type, called methods . For example, you can identify what a string looks like when all characters have been changed to upper case: >> s = "rover" >> s . upper () ’ROVER’ >> s ’rover’ This is a function that affects a primary argument ( s , the string). The function is bundled together with the string object s . To run the function, we use this dot notation, much like we used for the math . sqrt example before. 7.13 3
Image of page 3
Methods for an object To get a full listing of methods for an object, call the dir function: >> dir ( s ) [’__add__’, ’__class__’, ’__contains__’, ’__delattr__’, ’__doc__’, ’__eq__’, ’__ge__’, ’__getattribute__’, ’__getitem__’, ’__getnewargs__’, ’__getslice__’, ’__gt__’, ’__hash__’, ’__init__’, ’__le__’, ’__len__’, ’__lt__’, ’__mod__’, ’__mul__’, ’__ne__’, ’__new__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__rmod__’, ’__rmul__’, ’__setattr__’, ’__str__’, ’capitalize’, ’center’, ’count’, ’decode’, ’encode’, ’endswith’, ’expandtabs’, ’find’, ’index’, ’isalnum’, ’isalpha’, ’isdigit’, ’islower’, ’isspace’, ’istitle’, ’isupper’, ’join’, ’ljust’, ’lower’, ’lstrip’, ’partition’, ’replace’, ’rfind’, ’rindex’, ’rjust’, ’rpartition’, ’rsplit’, ’rstrip’, ’split’, ’splitlines’, ’startswith’, ’strip’, ’swapcase’, ’title’, ’translate’, ’upper’, ’zfill’] We won’t look at most of these in this course, but you might sometimes want to use them. 7.14 Modules and dir We can also use the dir command to investigate what is in a module, as well. Here’s a list of the functions and constants defined by the math module: >>> import math >>> dir ( math ) [’__doc__’, ’__file__’, ’__name__’, ’acos’, ’asin’, ’atan’, ’atan2’, ’ceil’, ’cos’, ’cosh’, ’degrees’, ’e’, ’exp’, ’fabs’, ’floor’, ’fmod’, ’frexp’, ’hypot’, ’ldexp’,
Image of page 4

Subscribe to view the full document.

Image of page 5
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern