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

D

Dave

Hi all,

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 "

I've found absolutely everything else that I expect from a modern
programming language, but none of the modules (not even "string"!)
seems to have what I'm looking for.

Please tell me I'm blind!

Dave
 
M

Michael Hudson

Hi all,

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 "

I've found absolutely everything else that I expect from a modern
programming language, but none of the modules (not even "string"!)
seems to have what I'm looking for.

Please tell me I'm blind!

Well, you're just not looking in the right place:
text = "If someone attacks you with a banana"
text[0:3]
'If '

I'm pretty sure this is in the tutorial -- the key word you're looking
for is "slice".

Cheers,
mwh
 
I

Irmen de Jong

Dave said:
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
 
C

Christopher Boomer

You are ;-) You couldn't find it in the documentation for the modules
because
it's a base operator on the string itself:

Surely a lesson to be learned there?

I am still a relative newcomer and regularly find myself stumbling round the
tutorial to find the answers that are too easy for the library reference.

Christopher Boomer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top