Review of Python-Strings & Lists (1)
Review of Python-Strings & Lists (1)
Strings in Python
Strings in Python
Strings are stored as individual characters in contiguous locations, with two-way index for each location.
Each character in a string can be accessed using its index number.
Example: x=“PYTHON”
0 1 2 3 4 5 Forward indexing x[0]=P=x[-6]
PYTHON x[1]=Y=x[-5]
-6 -5 -4 -3 -2 -1 Backward Indexing x[2]=T=x[-4]
x[3]=H=x[-3]
Length of the string can be determined by using len() function.
len(x) gives 6 as output.
Traversing a string means iterating the elements of a string using loops.
Example: x=“PYTHON”
for i in x:
print(i,end=“ “) Output is P Y T H O N
String Operators
+ (Concatenation Operator): Both operands should be same type.
Example: “Power”+”Ful” gives “PowerFul”, ‘20’+’10’ gives ‘2010’
*(Replication Operator) : one operand is a string and other operand should be a number. It
returns the repetition of a particular string to the corresponding number of times.
Example: ”Hai”*3 returns ‘HaiHaiHai’
Membership Operator(in & not in ): checks whether a particular character or substring is
present in the given string or not.
Example: ‘a’ in ‘hai’ returns True
‘a’ not in ‘apple’ returns False.
Comparison Operators(<, >,<=,>=,==,!=)