DataStructures Strings 15
DataStructures Strings 15
What is Information?
If we arrange some data in an appropriate sequence, then it forms a Structure and
gives us meaning. We found two things in Information: One is Data and the other is
Structure.
Example:String Repetition
print('Spam'*3)
Example:
MyStr="PYTHON"
print(MyStr)#PYTHON
MyStr="Machine Leaning"
print(MyStr)#Machine Leaning
Example:
PyStr="python"
print(PyStr)
print(PyStr+PyStr)
print(PyStr+" "+PyStr)
print(PyStr*3)
print(PyStr[0])
PyStr[0]='P'
print(PyStr)
Multiple Assignment
You can assign values to multiple python variables in one statement.
Example:
a=b=c=5
print(a);print(b);print(c)
Here two indices are used separated by a colon (:). A slice 3:7 means indices
characters of 3rd, 4th, 5th and 6th positions. The second integer index i.e. 7 is
not included. You can use negative indices for slicing.
Example:
PyStr="PYTHON IS FUN"
#P Y T H O N I S F U N
#0 1 2 3 4 5 6 7 8 9 10 11 12
#-13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
print(PyStr[0:2])
print(PyStr[2:6])
print(PyStr[7:10])
print(PyStr[1:8])
string1 ="PYTHON"
Character P Y T H O N
Index (from left) 0 1 2 3 4 5
Index (from right) -6 -5 -4 -3 -2 -1
Example:
PyStr="Naresh i Technologies"
print(PyStr[0])
print(PyStr[-1])
print(PyStr[-4])
print(PyStr[4])