w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python string slicing, special case if end of string included?

The issue here is -0 just is 0, so you're attempting to grab up to the first character of the string

so for the case of i = 6 you get

test[-2:0] = ''

a better way of handling this is look ahead

for i in range(len(test)-1):
     print test[i:i+2]

for indexing from the end to work the correct syntax would leave out the 0

test[-2:] = 'gh'

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.