Open In App
Related Articles

Output of Python Programs | Set 19 (Strings)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

1) What is the output of the following program? 
 

PYTHON3

str1 = '{2}, {1} and {0}'.format('a', 'b', 'c')
str2 = '{0}{1}{0}'.format('abra', 'cad')
print(str1, str2)

                    

a) c, b and a abracad0 
b) a, b and c abracadabra 
c) a, b and c abracadcad 
d) c, b and a abracadabra 
Ans. (d) 
Explanation: String function format takes a format string and an arbitrary set of positional and keyword arguments. For str1 ‘a’ has index 2, ‘b’ index 1 and ‘c’ index 0. str2 has only two indices 0 and 1. Index 0 is used twice at 1st and 3rd time.
2) What is the output of the following program? 
 

PYTHON3

a = 2
b = '3.77'
c = -8
str1 = '{0:.4f} {0:3d} {2} {1}'.format(a, b, c)
print(str1)

                    

a) 2.0000 2 -8 3.77 
b) 2 3.77 -8 3.77 
c) 2.000 3 -8 3.77 
d) 2.000 2 8 3.77 
Ans. (a) 
Explanation: At Index 0, integer a is formatted into a float with 4 decimal points, thus 2.0000. At Index 0, a = 2 is formatted into a integer, thus it remains to 2. Index 2 and 1 values are picked next, which are -8 and 3.77 respectively.
3) What is the output of the following program? 
 

PYTHON3

import string
 
Line1 = "And Then There Were None"
Line2 = "Famous In Love"
Line3 = "Famous Were The Kol And Klaus"
Line4 = Line1 + Line2 + Line3
print("And" in Line4)

                    

a) True 2 
b) True 
c) False 
d) False 2 
Ans. (b) 
Explanation: 
The “in” operator returns True if the strings contains the substring (ie, And), else returns False. 
4) What is the output of the following program? 
 

PYTHON3

line = "I'll come by then."
eline = ""
for i in line:
    eline += chr(ord(i)+3)
print(eline)   

                    

a) L*oo frph e| wkhq1 
b) L*oo#frph#e|#wkhq1 
c) l*oo@frph@e|$wkhq1 
d) O*oo#Frph#E|#wKhq1 
Ans. (b) 
Explanation: This piece of code ciphers the plain text. Each character is moved to its 3rd next character by increasing the ascii value. ‘I’ becomes ‘L’, thus option (c) and (d) are ruled out. ‘ ‘ has ascii value of 32, thus it’ll become 35(‘#’), thus option (a) is ruled out as, ‘ ‘ can not remain to be ‘ ‘ in the ciphered text.
5) What is the output of the following program? 
 

PYTHON3

line =  "What will have so will"
L = line.split('a')
for i in L:
    print(i, end=' ')

                    

a) [‘What’, ‘will’, ‘have’, ‘so’, ‘will’] 
b) Wh t will h ve so will 
c) What will have so will 
d) [‘Wh’, ‘t will h’, ‘ve so will’] 
Ans. (b) 
Explanation: split() will use ‘a’ as the delimiter. It’ll create partition at ‘a’, thus split() return an array L, which is in [‘Wh’, ‘t will h’, ‘ve so will’]. For loop will print the elements of the list.
 

 



Last Updated : 03 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads