472,354 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

print command don't work (subscripted) word[2:4]

Why is this not working ?

bla = 'hondenriem'
print bla[0:4] # correct ! (= hond)
print bla[3:2] # nothing ! (= en)
print bla[6:3] # nothing ! (= riem)

Why don't bla[3:2] and bla[6:3] won't work ?

I use this version:
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32

http://docs.python.org/tutorial/intr...n.html#strings
word = 'HelpA'
>>word[4]
'A'
>>word[0:2]
'He'
>>word[2:4]
'lp'
Oct 7 '08 #1
3 1152
gc*******@gmail.com wrote:
Why is this not working ?

bla = 'hondenriem'
print bla[0:4] # correct ! (= hond)
print bla[3:2] # nothing ! (= en)
print bla[6:3] # nothing ! (= riem)

Why don't bla[3:2] and bla[6:3] won't work ?

I use this version:
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32

http://docs.python.org/tutorial/intr...n.html#strings
word = 'HelpA'
>>>word[4]
'A'
>>>word[0:2]
'He'
>>>word[2:4]
'lp'
The 2nd number in a slice is the final position, not the number
of characters:
>>bla = 'hondenriem'
print bla[0:4]
hond
>>print bla[3:2] # returns nothing
>>print bla[3:3+2] # returns 2 characters from pos 3-5
de
>>print bla[6:6+3] # returns 3 characters from pos 6-9
rie
-tkc


Oct 7 '08 #2
gcmartijnWhy is this not working ?
gcmartijnbla = 'hondenriem'
gcmartijnprint bla[0:4] # correct ! (= hond)
gcmartijnprint bla[3:2] # nothing ! (= en)
gcmartijnprint bla[6:3] # nothing ! (= riem)

gcmartijnWhy don't bla[3:2] and bla[6:3] won't work ?

Because those two examples define zero-length slices (left index <= right).
The second number in each slice specification is the ending index of the
slice, not the desired length.
>>bla = 'hondenriem'
print bla[0:4]
hond
>>print bla[3:2]
>>print bla[2:3]
n
>>print bla[6:3]
>>print bla[3:6]
den
>>print bla[3:3]

Skip
Oct 7 '08 #3
On Tue, Oct 7, 2008 at 2:50 PM, <gc*******@gmail.comwrote:
Why is this not working ?

bla = 'hondenriem'
print bla[0:4] # correct ! (= hond)
print bla[3:2] # nothing ! (= en)
Why do you think the right answer is 'en'? bla[3:2] is asking for the
slice starting at position 3, ending before position 2, reading left
to right. If you were expecting the slice to read backwards from
position 3 to position 2, you'd need to supply a third argument to the
slice:
>>print bla[3:2:-1]
d

If you were expecting the second argument in the slice notation to be
the length of the substring, well, that's just wrong. Python slices
don't work that way. Take a look at the tutorial, particularly the
bit on slice notation, here:
http://docs.python.org/tutorial/intr...n.html#strings

--
Jerry
Oct 7 '08 #4

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

Similar topics

5
by: MouseHart | last post by:
I've written a simple program in VB 6.0 to list all my MP3 files. To show them on the screen I used an MSFlexGrid named TextGrid (which is not associated with any table or text file) in the...
0
by: KohlerTommy | last post by:
In my application I need to give the user the ability to print duplex if the selected printer supports duplex printing. Many of the printer options do not make much sense in my application, and...
2
by: Joe Black | last post by:
Hi My database is filling a Word docs with data using bookmarks. My client wants one particular document to print using the lower tray which will hav a special letterhead loaded in it. Is...
2
by: Code Monkey | last post by:
I'm about to deploy a program that prints out Word documents. However, due to licensing issues, we don't actaully have Word 2003 or Office 2003 installed on the machine - only the free Word Viewer...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
1
by: kirkus84 | last post by:
I am currently trying to do a multiple record mail merge through a query via a command button on a form. The query basically displays customers who have said yes to privacy. The user inputs a date...
2
by: alivip | last post by:
when I wont to inser (anyting I print) to the textbox it will not inser it just print then hanging # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, #...
12
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to...
1
by: riccardonews | last post by:
Hi groups, I would like to hook print driver for manage the file that the MS windows'user want to print. I don't want to manage EMF file that the print spooler generate.. I have generate a...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

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.