[Tutor] string codes

Walter Prins wprins at gmail.com
Thu Nov 28 02:12:36 CET 2013


Hi,


On 27 November 2013 21:20, spir <denis.spir at gmail.com> wrote:

> All in all, startswith plus start-index only seems to work fine, I guess.
> What is wrong? string.find also works (someone suggested it on the
> python-ideas mailing list) but requires both start- and end- indexes. Also,
> startswith returns true/false, which is more adequate conceptually for a
> checking instruction.
>

Sorry to wade in after all the other answers you've had, but a)
string.find() does not *require* start and end indexes, they are optional:
http://docs.python.org/2/library/string.html   And b) if you're just trying
to find out whether a substring exists in a string (which I'm getting the
impression you're all you're doing), you can use "in":

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 1.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

walter-desktop[c:\users\walterp\src]|1> s = "abcdefghi"
walter-desktop[c:\users\walterp\src]|2> 'bcd' in s
                                    <2> True
walter-desktop[c:\users\walterp\src]|3> 'fgh' in s
                                    <3> True
walter-desktop[c:\users\walterp\src]|4> 'iab' in s
                                    <4> False
walter-desktop[c:\users\walterp\src]|5> s.find('bcd')
                                    <5> 1
walter-desktop[c:\users\walterp\src]|6> s.find('fghi')
                                    <6> 5
walter-desktop[c:\users\walterp\src]|7>


Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131128/eb6bd0af/attachment.html>


More information about the Tutor mailing list