Assignment 1700480105
Assignment 1700480105
STRINGS
22 string methods/functions:
isupper(), endswith() capitalize(), lstrip(), count(), replace() split(),
islower(), startswith() title(), rstrip(), find(), join() partition()
isalnum(), upper(), strip(), index(),
isalpha(), lower(),
isdigit(),
isspace(),
REVIEW OF CHAPTER-9
we learnt: 12 Questions
&
Ex:
a=”cs lab”
print(a.isupper()) o/p: False
Syntax: string.isupper()
startswith() function
checks whether the string starts with a particulat substring(i.e
specified value) and returns either True or False
Ex:
a=”cs lab” o/p: False
print(a.startswith(“a”))
capitalize(), title(),
upper(), lower(),
strip(), lstrip(), rstrip()
capitalize() function
● First, capitalize() function makes a copy of the given string
● Then, it converts the first letter of the copied string to capital.
● Finally, it returns the manipulated copy as result.
Ex:
a=”CS LAB”
print(a.capitalize()) o/p: Cs lab
print(a) CS LAB
Syntax: string.capitalize()
title() function
● First, title() function makes a copy of the given string
● Then, it converts the first letter of each word in the copied string to capital.
● Finally, it returns the manipulated copy as result.
Ex:
a=”CS LAB” o/p: Cs Lab
print(a.title()) CS LAB
print(a)
Syntax: string.title()
upper()
● First, upper() function makes a copy of the given string
● Then, it converts all the letters present in the copied string to capital.
● Finally, it returns that manipulated copy as result.
Ex:
a=”cs lab” o/p: CS LAB
print(a.upper()) cs lab
print(a)
Syntax: string.upper()
strip(), lstrip(), rstrip()
● First, strip() function makes a copy of the given string
Note: by default,
3 characters(space, tabspace and newline)
will be passed as input to strip() function
txt = ",,,,,rrttgg.....cricket....rrr"
x = txt.strip(",.grt")
print(x) o/p:
print(txt) cricke
,,,,,rrttgg.....cricket....rrr
working:
strip(“,.grt”) function checks whether any of these 5 characters are
present in the beginning and at the end of the copied string.
find(), index()
count(),
join()
find()
Ex:
a=”computer science lab”
o/p:
print(a.find(‘o’)) 1
print(a.find(‘e’)) 6
Description:
find() function checks whether the specified value(i.e substring) is present in the given string
or not.
Description:
index() function checks whether the specified value(i.e substring) is present in the given
string or not.
Description:
count() function returns the number of times the specified value
(i.e substring) appears in the given string.
example:
str= “Exam 2023”
b=str.join(‘###’) o/p:
print(b) #Exam 2023#Exam 2023#
print(str) Exam 2023
Question-4 2 MARKS
Explain the following 3 functions:
replace(),
partition(),
split()
replace()
Syntax: string.replace(old substring, new substring)
Description
● First, replace() function makes a copy of the given string
● Then, it replaces the specified old substring with the new substring in the
copied string
example:
str= 'dododo' o/p:
b=str.replace('do', 'c') ccc cococo dododo
c=str.replace('d', 'c')
print(b, c, str)
split() partition()
Description Description
split() function splits copy of the partition() function splits copy of the
given string into a list of words given string always into tuple of 3 words
using the specified separator. using the specified separator.
1st word - content before the separator
Note: by default,
2nd word - separator
space, tabspace and newline 3rd word - content after the separator
characters will be passed as input to
separator split() function
Syntax
string.split(separator=’ ‘, maxsplit=) Syntax
separator is optional parameter string.partition(separator)
maxsplit is optional parameter. (No of separator is compulsory parameter
splits is the input)
#EXAMPLE-1A: #EXAMPLE-2A: #EXAMPLE-3A:
txt = "welcome to the jungles" txt = "welcome to the jungles" s ="apple#grapes#cherry#orange"
#EXAMPLE-3B:
s ="apple#grapes#cherry#orange"
#EXAMPLE-1B: #EXAMPLE-2B:
txt = "welcome to the jungle" txt = "welcome to the jungle" res = s.partition("#")
res = s.split("#", 2)
2A O/P: ['welcome', 'to', 'the', 'jungles'] 3A O/P: ['apple', 'grapes', 'cherry', 'orange']
2B O/P: ERROR because no input is passed to separator 3B O/P: ('apple', '#', 'grapes#cherry#orange')
program:(using indexing)
program: a=input(“Enter the string:”)
a=input(“Enter the string:”) c=0
c=0 for b in range(0, len(a)):
for b in a: if a[b] in “aeiou”:
if b in “aeiou”: c=c +1
c=c +1
print(“No of vowels=”, c)
print(“No of vowels=”, c)
.
PROGRAM-5b
Write a Python program to check whether the given string is
OUTPUT-1
palindrome or not. PALINDROME CHECKER
Enter any string: dad
OUTPUT-2
PALINDROME CHECKER
program: Enter any string: python
b=a[::-1]
if a==b:
print(“The given string is palindrome “)
else:
print(“The given string is not palindrome “)
OUTPUT
QUESTIONS
6a. Find and write the output of the following python code :
Msg1="WeLcOME"
Msg2="GUeSTs"
Msg3=""
for I in range(0,len(Msg1):
if Msg1[I]>="A" and Msg1[I]<="M":
Msg3=Msg3+Msg1[I]
elif Msg1[I]>="N" and Msg1[I]<="Z":
Msg3=Msg3+Msg2[I]
else:
Msg3=Msg3+"*"
s='School2@com'
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+'bb'
o/p:
print(m) sCHOOLbbbbCOM
6c Find and write the output of the following python code :
m=""
str='[email protected]'
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#" o/p:
fUN#pYTHONn#.
print(m)
Last two are
THEORY QUESTIONS
Question-7 2 MARKS
what is String Traversing?
example:
inorder to find the number of vowels present in the
given string, each and every character has to be
processed. This activity is called as Traversing.
Question-8 2 MARKS
List out String operations.
4 string operations
● concatenation
● replication
● membership ex: if b in “aeiou”:
● slicing