Practicals On String
Practicals On String
s1="Kendriya"
print(s1) Kendriya
s1[1]='f' s1[1]='f'
print(s1) TypeError: 'str' object does not support item assignment
String datatype is immutable and can not change once
declared.
#string with membership operator
s1="Kendriya"
print('d' in s1) True
print('d' not in s1) False
print('N' in s1) False
print('n' not in s1) False
# CONCATENATING STRING
s1="Kendriya"
s2="Vidyalaya"
s3=s1+s2
print(s3) KendriyaVidyalaya
#REPLICATING OF STRING
s1="Kendriya"
s2=s1*3
print(s2) KendriyaKendriyaKendriya
# USE OF \n AND \t
print("KENDRIYA \n VIDYALAYA\t AUGUSTYAMUNI") KENDRIYA
VIDYALAYA AUGUSTYAMUNI
# Traversing in string K
S= "KVAU" V
for i in S: A
print(i) U
# TO COUNT NUMBER OF TIMES U/u OCCUR IN A # spilt() function
STRING KVAUGUSTYAMUNI
s1="kendriya vidyalaya augustyamuni"
S= "KVAUGUSTYAMUNI" words=s1.split()
n=0 print(words)
for i in S:
if i=="U" or i=="u": OUTPUT:
n=n+1 ['kendriya', 'vidyalaya', 'augustyamuni']
print("Number of Times U/u is =",n)
OUTPUT:-
Number of Times U is = 3 Note:- return in the form of list datatype
# To count no of words in the sentence # To count no of times “am” in the sentence