Isn't there a substring(start, end)-function????

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Wed Aug 6 08:23:58 EDT 2003


Dave wrote:

> Am I blind, or what? I can't find any quick way to do the following in
> Python:
> 
> substring(beginIndex, endIndex) witch returns the substring between
> beginIndex and endIndex.
> 
> Like:
> text = "If someone attacks you with a banana"
> print text.substring(0,3)
> Should print "If "

You're not blind but you've overlooked string slicing:

C:\> python
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> text = "If someone attacks you with a banana"
 >>> print text[0:3]
If
 >>> print text[3:10]
someone
 >>> print text[-6:]
banana
 >>> print text[::-1]
ananab a htiw uoy skcatta enoemos fI


more info here: http://www.python.org/doc/current/ref/slicings.html

--Irmen





More information about the Python-list mailing list