Accessing Values in Strings and Updating

var = 'Python Programming'
print "var[0]: ", var[0]
print "var[3:9]: ", var[3:9]
print "var[1:5]: ", var[1:5]
print "var[:]: ", var[:]           # List all elements in string
print "var[:2]: ", var[:2]       # List first 2 elements in string
print "var[2:]: ", var[2:]      # Except first 2 elements in string
print "var[::-1]: ", var[::-1]  # String Reverse
j=len(var)
print 'Length of string is:',j
i=0
while i<j:
      print 'Character in the string is:', var[i]
      i=i+1

x = 'Hello World!'

print "Updated String :- ", 'New ' + x[6:12]

No comments: