Unit 4
Unit 4
String,list,tuple,Dictionary
STRINGS
String is sequence of characters are
surrounded by either single quotation marks,
or double quotation marks.
String is immutable
'hello' is the same as "hello".
The element is accessed by positive or
negative indexing.
STRING LENGTH
To get the length of a string, use
the len() function.
Example
The len() function returns the length of a
string:
a = "Hello, World!"
print(len(a))
OUTPUT:12
FIVE OPERATIONS CAN BE
PERFORMED ON STRING
Indexing
slicing
Concatenation
Repetition
membership
OPERATION 1: INDEXING
A=‘hello’
Slicing expression
l4 [1:4] Output: [4,6,22]
L4[3:-3] Output: [22,44]
L4[3:] Output:[22,44,12,55,66]
L4[:4] Output: [1,4,6,22]
EXAMPLES ON SLICING OPERATIONS
REMOVE THE FIRST AND LAST
CHARACTER FROM STRING
a = ‘python’
Print(a[1:-1])
Output: ytho
SWAP THE FIRST AND LAST
CHARACTER FROM STRING
A=‘active’
A[0:1] #a
A[-1] #e
A[1:-1:1] #ctiv
Concatenate: A[-1]+A[1:-1:1]+A[0:1]
OUTPUT:ectiva
TYPE3: CONCATENATION
Combining two string into one string.
A=python
B=programming