Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Isn't there a substring(start, end)-function????

Reply
Thread Tools

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

 
 
Dave
Guest
Posts: n/a
 
      08-06-2003
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
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      08-06-2003
(Dave) writes:

> 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

--
[2. More type system hacking --- text/plain; type-argh.diff]
-- csr on sbcl-devel
 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      08-06-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

 
Reply With Quote
 
Christopher Boomer
Guest
Posts: n/a
 
      08-06-2003
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a java.util.Scanner that works in 1.4.2 out there? Danno Java 2 04-26-2006 04:41 AM
Are there any Belkin router users out there? OM UK VOIP 12 10-04-2005 08:32 AM
is there a hardware / driver combination out there the lets win2k connect to the network before login?? christiane kewitz Wireless Networking 1 02-13-2005 01:08 AM
I am getting loads of spam by e-mail.Most of it is not even addressed to me so god knows how I am receiving it. Any ideas . Is there a spam guard available on blueyonder? How do I get it on my system if there is?Cheers GW Geoff/Elaine Computer Support 11 11-16-2004 11:17 PM
Are there any free .pdf writers out there Goonigoogoo Computer Support 5 11-22-2003 03:27 AM



Advertisments