Consider the following string mySubject:
mySubject = "Computer Science"
What will be the output of the following string operations:-
i. print(mySubject[0:len(mySubject)])
ii. print(mySubject[-7:-1])
iii. print(mySubject[::2])
iv. print(mySubject[len(mySubject)-1])
v. print(2*mySubject)
vi. print(mySubject[::-2])
vii. print(mySubject[:3] + mySubject[3:])
viii. print([Link]())
ix. print([Link]('Comp'))
x. print([Link]())
ANSWER:
The output of the given string operations are as follows:
i. Computer Science
ii. Scienc
iii. Cmue cec
iv. e
v. Computer ScienceComputer Science
vi. eniSrtpo
vii. Computer Science
viii. cOMPUTER sCIENCE
ix. True
x. False
Question 2:
Consider the following string myAddress:
myAddress = "WZ-1,New Ganga Nagar,New Delhi"
What will be the output of following string operations :
i. print([Link]())
ii. print([Link]())
iii. print([Link]('New'))
iv. print([Link]('New'))
v. print([Link]('New'))
vi. print([Link](','))
vii. print([Link](' '))
viii. print([Link]('New','Old'))
ix. print([Link](','))
x. print([Link]('Agra'))
ANSWER:
The output of the given string operations are as follows:
i. wz-1,new ganga nagar,new delhi
ii. WZ-1,NEW GANGA NAGAR,NEW DELHI
iii. 2
iv. 5
v. 21
vi. ['WZ-1', 'New Ganga Nagar', 'New Delhi']
vii. ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
viii. WZ-1,Old Ganga Nagar,Old Delhi
ix. ('WZ-1', ',', 'New Ganga Nagar,New Delhi')
x. Error .. Substring Not found
Q1: WAP to display each character in same line.
Sol:
st= input(“Enter String”)
for ch in st:
print(ch, end=’ ‘)
Q2: WAP to count character ‘e’ in the given string.
Sol:
st= input(“Enter String”)
c=0
for ch in st:
if(ch==’e’):
c=c+1
print(“Occurrence of Character e is - ”,c)
Q3: WAP to replace character ‘l’ with character ‘*’ in the given string.
Ex:
Input: Hello World!
Output: He**o Wor*d
Sol:
st= input(“Enter String”)
for ch in st:
if(ch==’e’):
ch=’*’
print(ch , end=’ ‘)
Q4: WAP to replace vowels with character ‘*’ in the given string.
Ex:
Input: Hello World!
Output: H*ll* W*rld
Sol:
st= input(“Enter String”)
for ch in st:
if(ch in ’AEIOUaeiou’):
ch=’*’
print(ch , end=’ ‘)
Q5: WAP to count total number of words present in the given string.
Input: Have a nice Day
Output: 4
Sol:
st= input(“Enter String”)
sp=0
for ch in st:
if(ch==’ ‘):
sp=sp+1
print(“Total number of words are:”(sp+1))
Q6: WAP to display string in reverse order.
Input: Have a nice Day
Output: yaD ecin a evaH
Sol:
st= input(“Enter String”)
length= len(st)-1
for ch in range(-1,length, -1):
print(st[ch], end=’ ‘)