0% found this document useful (0 votes)
6 views3 pages

Practicals On String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Practicals On String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICALS ON STRING:-

#STRING SLICING # OUTPUT


s1="Kendriya"
s2="Vidyalaya"
s3=s1+s2
print(s3) KendriyaVidyalaya
print(s3[0:8:1]) Kendriya
print(s3[1:10:2]) ediai
print(s3[-1:-9:-1]) ayalaydi
print(s3[::]) KendriyaVidyalaya
print(s3[0:8:]) Kendriya
print(s3[1::]) endriyaVidyalaya
print(s3[:8:]) Kendriya
# INDEXING IN STRING
S= "KVAUGUSTYAMUNI"
print(S[2]) A
print(S[-12]) A
print(S[4]) G
print(S[-10]) G
# STRING COMPARISION #OUTPUT
A="star"
B="ssar"
print(A>B) True
print(A==B) False
print(A<B) False
# STRINGS ARE IMMUTABLE

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

s1="kendriya vidyalaya augustyamuni" s1="I am Aneesh. I am 12 years old. I live in India"


words=s1.split() words=s1.split()
count=0 count=0
for i in words: for i in words:
count=count+1 if i=="am":
print("No of words in the string=",count) count=count+1
print("No of times “am” in the string=",count)
OUTPUT:- OUTPUT:-
No of words in the string= 3 No of times “am” in the string = 2
#STRING METHODS OR BUILT IN FUNCTIONS
OUTPUT
s1="kendriya vidyalaya"
print(len(s1)) 18
print(s1.capitalize()) Kendriya vidyalaya
print(s1.split()) ['kendriya', 'vidyalaya']
print(s1.replace("ya","YA")) kendriYA vidYAlaYA
print(s1.title()) Kendriya Vidyalaya
print(s1.lower()) kendriya vidyalaya
print(s1.upper()) KENDRIYA VIDYALAYA
print(s1.count("a")) 4
print(s1.find("a")) 8
print(s1.index("a")) 8
print(s1.endswith("a")) False
print(s1.startswith("a")) False
print(s1.isalnum()) False
print(s1.isalpha()) False
print(s1.isdigit()) False
print(s1.islower()) True
print(s1.isupper()) False
print(s1.isspace()) False
print(s1.lstrip()) kendriya vidyalaya
print(s1.rstrip()) kendriya vidyalaya
print(s1.strip()) kendriya vidyalaya
print("A".join("CDE")) CADAE
print("A".join("B")) B
print(s1.partition("a")) (' kendriy', 'a', ' vidyalaya ')

You might also like