Python String Test (CS)
Python String Test (CS)
School, Ch-92
Class: XII Computer Science- Worksheet
Write the output of the following code segment.
1.
I=0
Info='JustNow'
N=2
NewInfo=''
while I<len(Info):
if I%2==0:
NewInfo+= chr(ord(Info[I])-N)
elif Info[I].islower():
NewInfo+= Info[I].upper()
else:
NewInfo+=chr(ord(Info[I])+N)
I=I+1
print('New String:',NewInfo)
2.
i=0
newstr=''
s= "SUCCESS"
print("Original String",s)
while i<len(s):
if i%2!=0 and s[i]!=s[i+1]:
newstr=newstr+'@'
elif s[i]==s[i+1]:
newstr=newstr+s[i]+'!'
i+=1
else:
newstr=newstr+s[i]
i+=1
print("Changed String",newstr)
3.
Given the following code, What will be the output produced if the input is
(i) “abccd” (ii) bbbb (iii) “abccA” (iv) “Python”
s=input(“enter a string”)
c=0
while c>=0:
if s[0]=='a':
ns=s[1:]
if s[-1]!='A':
ns=s[:-1]
else:
ns=s[::-1]
c=c-1
print(ns,c,sep=":")
4.
a. Find the output of the following code segment [2]
Mystring=list("What@OUTPUT!") ;
for i in range(len(Mystring)):
if (not Mystring[i]. isalpha()):
Mystring[i] = '*'
elif Mystring[i].isupper():
Mystring[i]= chr(ord(Mystring[i]) +1)
else:
Mystring[i]= Mystring[i+1];
print(Mystring)
5.Find and write the output of the following python code:
msg = "Technology 2025"
print(msg[3:])
print(msg[:4],msg[4:])
print(msg[::-1])
print(msg[0:4],msg[11:10])
print(msg[-2:-10:-2]+msg[-5:-1])
print(msg[2:-2:3][3])
6.
What will be the output of the following code:
mystr="HARMONIOUS"
L = len(mystr)
str2=''
str3=''
for i in range(0,L,2):
str2=str2 + mystr[i+1]+mystr[i]
for ch in str2:
if ch>='R' and ch<='U':
str3+='$'
else:
str3+=ch.lower()
print(str3)
7. Find and write the output of the following python code: [4]
Numbers=[9, 18, 27, 38]
for Num in Numbers:
for N in range(1, Num%8) :
print(N, "#", end = "\n")
Given String STR=”COMPUTER”, choose the correct option(s) to print 'TUP'
string. [1] (i)STR[5:2:1] ii) STR[len(STR)-5:-2)] (iii) STR[-3:-6:1]
(iv) STR[- 3:-6:-1]
How many times the for loop prints the star(*) in the following code? [1]
for i in range(10,0,-2):
for j in range(1,i,2):
print('*')