PYTHON STRINGS
Strings are the simplest and easy to use in Python.String pythons are immutable.
We can simply create Python String by enclosing a text in single as well as double quotes. Python treat both single and double quotes statements same.
Accessing Strings:
- In Python, Strings are stored as individual characters in a contiguous memory location.
- The benefit of using String is that it can be accessed from both the directions in forward and backward.
- Both forward as well as backward indexing are provided using Strings in Python.
- Forward indexing starts with 0,1,2,3,....
- Backward indexing starts with -1,-2,-3,-4,....
str[0]='P'=str[-6] , str[1]='Y' = str[-5] , str[2] = 'T' = str[-4] , str[3] = 'H' = str[-3]
str[4] = 'O' = str[-2] , str[5] = 'N' = str[-1].
Simple program to retrieve String in reverse as well as normal form.
name="Rajat"
length=len(name)
i=0
for n in range(-1,(-length-1),-1):
print name[i],"\t",name[n]
i+=1
Post a Comment