String slices work only for first string character ?

Tim Chase python.list at tim.thechases.com
Tue Dec 16 14:42:59 CET 2008


> Can any one explain why the following string slice works only for the first character, but not for any other ?
> 
> $ cat /tmp/tmp.py
> #!/usr/bin/env python
> 
> data = 'F0023209006-0101'
> print data
> print "|"+data[0:1]+"|"
> print "|"+data[1:1]+"|"
> print "|"+data[2:1]+"|"
> 
> $ python `cygpath -w /tmp/tmp.py`
> F0023209006-0101
> |F|
> ||
> ||

Slices are defined by [start_idx:end_idx] not [start_idx:length] 
so you want

   data[0:1]
   data[1:2]
   data[2:3]

-tkc






More information about the Python-list mailing list