469,950 Members | 2,348 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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 1080
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by MouseHart | last post: by
reply views Thread by KohlerTommy | last post: by
2 posts views Thread by Code Monkey | last post: by
13 posts views Thread by Chris Carlen | last post: by
1 post views Thread by riccardonews | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.