Question: 6 -
What will be the output of below Python code?str1="poWer"
str1.upper()
print(str1)
-
power
-
poWer
-
Power
-
POWER
Answer:
poWer
Solution:
str1.upper() returns the uppercase of whole string str1. However,it doesnot change the string str1. So, output will be the original str1.
str1.upper() returns the uppercase of whole string str1. However,it doesnot change the string str1. So, output will be the original str1.
Question: 7 -
What will following Python code return?str1="Stack of books"
print(len(str1))
-
13
-
16
-
14
-
15
Answer:
14
Solution:
len() returns the length of the given string str1, including spaces and considering " " as a single character.
len() returns the length of the given string str1, including spaces and considering " " as a single character.
Question: 8 -
What will the below Python code will return?list1=[0,2,5,1]
str1="7"
for i in list1:
str1=str1+i
print(str1)
-
15
-
Error
-
7
-
70251
Answer:
Error
Solution:
list1 contains integers as its elements. Hence these cannot be concatenated to string str1 by simple "+" operand. These should be converted to string first by use of str() function,then only these will get concatenated.
list1 contains integers as its elements. Hence these cannot be concatenated to string str1 by simple "+" operand. These should be converted to string first by use of str() function,then only these will get concatenated.
Question: 9 -
Guess the correct output of the following code?str1 = "PYnative"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
-
None of these
-
PYn PYnat ive PYnativ vitanYP
-
Yna PYnat tive PYnativ vitanYP
-
Yna PYnat tive PYnativ PYnativ
Answer:
Yna PYnat tive PYnativ PYnativ
Solution:
We can use a slice operator [] to get a substring.
Syntax: s[start : end]
We can use a slice operator [] to get a substring.
Syntax: s[start : end]
Question: 10 -
Which of the following will give "Simon" as output?If str1="John,Simon,Aryan"
-
print(str1[-11:-6])
-
print(str1[-7:-11])
-
print(str1[-11:-7])
-
print(str1[-7:-12])
Answer:
print(str1[-11:-6])
Solution:
Slicing takes place at one index position less than the given second index position of the string. So,second index position will be -7+1=-6.
Slicing takes place at one index position less than the given second index position of the string. So,second index position will be -7+1=-6.