> -----Original Message----- > Date: Mon, 25 Sep 2006 02:59:45 -0700 > From: Dick Moores <[EMAIL PROTECTED]> > Subject: [Tutor] Question about startswith() and endswith() in 2.5 > To: tutor@python.org > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="us-ascii"; format=flowed > > http://www.python.org/doc/lib/string-methods.html has > ============================================= > startswith( prefix[, start[, end]]) > Return True if string starts with the prefix, otherwise return False. > prefix can also be a tuple of suffixes to look for. With optional > start, test string beginning at that position. With optional end, > stop comparing string at that position. > > Changed in version 2.5: Accept tuples as prefix. > ============================================== > > and > > ================================================ > endswith( suffix[, start[, end]]) > Return True if the string ends with the specified suffix, otherwise > return False. suffix can also be a tuple of suffixes to look for. > With optional start, test beginning at that position. With optional > end, stop comparing at that position. > > Changed in version 2.5: Accept tuples as suffix. > ================================================== > > Through experimentation I now see a use for a tuple in which start > and end are indexes (as with the startswith() and endswith() of 2.4.3): > > >>> s = "qwerty" > >>> > >>> s.startswith("er",2,3) > False > >>> > >>> s.startswith("er",2,4) > True > >>> > > but > >>> s.startswith("er","q","ty") > > Traceback (most recent call last): > File "<pyshell#55>", line 1, in <module> > s.startswith("er","q","ty") > TypeError: slice indices must be integers or None or have an __index__ > method > > On http://docs.python.org/whatsnew/other-lang.html I found > > ================================================== > The startswith() and endswith() methods of string types now accept > tuples of strings to check for. > > > def is_image_file (filename): > return filename.endswith(('.gif', '.jpg', '.tiff')) > > ==================================================== > > This is the only example I've been able to find in the documentation > that uses the new tuple of strings, and I don't understand it. The > function is_image_file() will return filenames ending in '.gif', but > what do '.jpg' (as start) and '.tiff' (as end) do? What kind of > data(?) would this function be applied to? A Python list of filenames? > > Thanks, > > Dick Moores >
Hello, Dick. Let's compare your final startswith method and the endswith method in is_image_file: >>>>>>> s.startswith("er","q","ty") filename.endswith(('.gif', '.jpg', '.tiff')) >>>>>>> Notice that, while startswith has THREE parameters, endswith has only ONE. ('.gif', '.jpg', '.tiff') is a tuple, and the interpreter sees it as a single parameter. In other words your method is passing the following parameters: prefix = "er" start = "q" end = "ty while the example method is passing: suffix = ('.gif', '.jpg', '.tiff') start = None end = None Does that make sense? Good luck. Regards, Barry [EMAIL PROTECTED] 541-302-1107 ________________________ We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor