Different indexing in string

fruit='apples'
l=len(fruit)
print 'Length of string:',l
print  fruit[0:4]    #[m:n]  m is starting index and n will upto n-1 index
print  fruit[-4]      # Index starting from last element as -1, second last element as -2 so on
print  fruit[::-1]   # Reverse of a string
print fruit[:2]
print fruit[2:]

try:
      print  fruit[6]
except:
      print 'index error'

for i in fruit:
      print '\nChar in the string: ',i

for j in range(l):
      print  '\nIndex and Char in the string: ',j,fruit[j]


No comments: