CS CH2 PR1 Xii
CS CH2 PR1 Xii
(Practical 1)
STRINGS in Python:
Strings in Python are stored as individual characters in contiguous locations, with two-way
index for each location.
Example:
Str= “Python Programming”
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
P y t h o n P r o g r a m m i n g
Note: Strings are immutable and hence item assignment is not allowed / supported.
1
# PRG1: Program to test that strings are IMMUTABLE
Str="Python Programming"
print('='*57)
print(Str)
print('-'*57)
try:
Str[0]='H'
print(Str)
except:
print("TypeError: 'str' object does not support item assignment")
print('='*57)
Output:
=========================================================
Python Programming
---------------------------------------------------------
TypeError: 'str' object does not support item assignment
=========================================================
Traversing a String:
Traversing refers to iterating through the elements of a string one character at a time.
print('='*57)
Str="Python Programming"
print(Str)
print('-'*57)
for ch in Str:
print(ch,'-',end=' ')
print('\n','-'*57)
d=len(Str)
e= -d
for i in range (0,d):
print("Str[",i,"]=",Str[i],"\tStr[",e,"]=",Str[e])
e+=1
print('='*57)
2
Output:
======================================================================
Python Programming
----------------------------------------------------------------------
P-y-t-h-o-n- -P-r-o-g-r-a-m-m-i-n-g-
----------------------------------------------------------------------
Str[ 0 ]= P Str[ -18 ]= P
Str[ 1 ]= y Str[ -17 ]= y
Str[ 2 ]= t Str[ -16 ]= t
Str[ 3 ]= h Str[ -15 ]= h
Str[ 4 ]= o Str[ -14 ]= o
Str[ 5 ]= n Str[ -13 ]= n
Str[ 6 ]= Str[ -12 ]=
Str[ 7 ]= P Str[ -11 ]= P
Str[ 8 ]= r Str[ -10 ]= r
Str[ 9 ]= o Str[ -9 ]= o
Str[ 10 ]= g Str[ -8 ]= g
Str[ 11 ]= r Str[ -7 ]= r
Str[ 12 ]= a Str[ -6 ]= a
Str[ 13 ]= m Str[ -5 ]= m
Str[ 14 ]= m Str[ -4 ]= m
Str[ 15 ]= i Str[ -3 ]= i
Str[ 16 ]= n Str[ -2 ]= n
Str[ 17 ]= g Str[ -1 ]= g
======================================================================
String Operators:
There are some operators that can be used to manipulate strings in multiple ways.
i) String concatenation operator (+)
ii) String replication operator (*)
iii) Membership operators (in and not in)
iv) Comparison operators (<, >, <=, >=, ==, !=)
3
# PRG3 : Write program to demonstrate the use of string operators.
print('='*57)
Str1="Hello"
Str2="Welcome to"
Str3="Python Programming"
Temp=Str2+' '+Str3
print(Temp)
print('-'*57)
print(Str1*5)
print('-'*57)
print(Str2 in Temp)
print(Str3 not in Temp)
print(Str1 in Temp)
print('-'*57)
print(Str1=="Hello")
print(Str1=="hello")
print('='*57)
Output:
=========================================================
Welcome to Python Programming
---------------------------------------------------------
HelloHelloHelloHelloHello
---------------------------------------------------------
True
False
False
---------------------------------------------------------
True
False
=========================================================