[Tutor] slices

Will Harris mosinu at gmail.com
Thu Nov 17 18:49:06 CET 2005


Blah brain freeze I saw my mistake as I hit send.
It should just be word[-2:]

So

#!/usr/bin/python
import sys

for word in sys.argv[1:]:
print word[-2:]

Works now. Thanks!

On 11/17/05, Will Harris <mosinu at gmail.com> wrote:
>
> Is there an easy way to slice the last set of characters off a string when
> I don't know the length of the string? For example say I have a list of
> words like
> this
> though
> weather
> I want to get the last 2 characters from each. But so far nothing I have
> tried has seemed to work. Currently I am doing something like:
>
> #!/usr/bin/python
> import sys
>
> for word in sys.argv[1:]:
> print word[-3:-1]
>
> I have tried to change the numbers around but I always seem to end up at
> the wrong place of the string. If I new the length each time I could just do
> a positive.
> Doesn't this work in the form of:
>
> t h i s
> 0 1 2 3
> -4 -3 -2 -1
>
> Did I miss something somewhere in some tutorial that someone would be kind
> enough to point me to?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051117/95e00ead/attachment.htm


More information about the Tutor mailing list