[Tutor] conditionals with slicing not seeming to work

Kent Johnson kent37 at tds.net
Mon Jan 28 04:19:55 CET 2008


Rob Stevenson wrote:

> the intention of this snippet is to only print slices where character 1 
> is lower case, 2-4 and 6-8 are upper.  The logic here looks right to a 
> VB eye.

> for i in h:
>     j=s[i:i+8]
>     if j[0].islower():
>         if j[1:3].isupper():
>           if j[5:7].isupper():
>               print j
> 
> Any help much appreciated!

If you want the second character through the fourth (three characters), 
use j[1:4]. Slices include the first index, not the second - they are 
'up to but not including' the end index.

Likewise j[5:8] would be the sixth through eigth characters.

Kent


More information about the Tutor mailing list