Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of Python Programs | Set 19 (Strings)

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Last Updated : 03 Aug, 2021
Like Article
Save Article
Similar Reads