473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jul 18 '05 #1
3 46721
da*****@email.com (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
Jul 18 '05 #2
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

Jul 18 '05 #3
> 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.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Don Freeman | last post by:
Seems like whatever value I use for the first int field (starting position) the substring procedure negates it and triggers a String index out of range error. I've tried all sorts of work...
7
by: cpp_weenie | last post by:
Given a std::string of the form "default(N)" where N is an integer number of any length (e.g. the literal string might be "default(243)"), what is the quickest way to extract the characters...
1
by: sysindex | last post by:
I am trying to find a way to dynamically retrieve the substring starting point of an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
0
by: Gazi Mahmud | last post by:
I am trying to find a way to dynamically retrieve the substring starting point for an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
6
by: dennishancy | last post by:
Trying to debug someone else's javascript code. There is a line that looks like this: req = req.substring(req.length-2, req.length); >From what I can gather in their comments, they want to...
6
by: dreamerbin | last post by:
Hi, I'm having trouble extracting substrings using regular expression. Here is my problem: Want to find the substring that is immediately before a given substring. For example: from "00...
2
by: mallard134 | last post by:
Could someone please help a newbee vb programmer with a question that is driving me crazy. I am trying to understand a line of code that is supposed to return the domain portion of a valid email...
1
by: m.k.ball | last post by:
What is the best way to achieve alphabetically ordered lists that will index phrases starting with 'The' as if the word was at the end rather than the start of the phrase? Michael
4
by: samuelberthelot | last post by:
Hi, I would like to extract 'some string' from the following string: <A href=\"#\">some string</A> Attention! This is not an HTML element, it is a STRING, so I can't do ..innerHTML. 'some...
1
by: rh1200la | last post by:
Hey all. I'm doing a very basic screen scrape of a page that displays a banner ad. It is basically an <Atag and an <IMGtag. I need to grab the url of each and store them. Right now I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.