Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
88 views

Python String Test (CS)

The document contains 7 coding questions related to Python string manipulation and loops. The questions cover topics like string slicing, indexing, splitting, concatenation, changing case, counting words and characters, reversing strings etc. Sample code snippets and inputs are provided to find the outputs.

Uploaded by

Jungjun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Python String Test (CS)

The document contains 7 coding questions related to Python string manipulation and loops. The questions cover topics like string slicing, indexing, splitting, concatenation, changing case, counting words and characters, reversing strings etc. Sample code snippets and inputs are provided to find the outputs.

Uploaded by

Jungjun
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Chinmaya Vidyalaya Sr. Sec.

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('*')

Name the exceptions thrown by the following statement [1]


(i) print(str(true)) (ii) while i in 'Hello'
With the Given string s='SEven Wonders7', Write the most appropriate statement
to perform the following task: [3]
(i) To extract 'Wonders' from s
(ii) To change the letter 'e' to 'X'
(iii) To return the position of 'even in s
(iv) To remove trailing spaces in s
(v) To find the number of words in s # Do not write program. Only one line
(vi) To display the string in reverse order.

(ii) Write the output of following code segments. [2]


s ='Just Do It'
w = s.split()
c = list(w[0]+w[1]+w[2])
print(w) print(c)

You might also like