Question: 1 -
What will be the output of below Python code?str1="Information"
print(str1[2:8])
-
formatio
-
format
-
orma
-
ormat
Answer:
format
Solution:
Concept of slicing is used in this question. In string slicing,the output is the substring starting from the first given index position i.e 2 to one less than the second given index position i.e.(8-1=7) of the given string str1. Hence, the output will be "format".
Concept of slicing is used in this question. In string slicing,the output is the substring starting from the first given index position i.e 2 to one less than the second given index position i.e.(8-1=7) of the given string str1. Hence, the output will be "format".
Question: 2 -
Which of the following will result in an error?str1="python"
-
str1[1]="x"
-
None of these
-
print(str1[0:9])
-
print(str1[2])
Answer:
str1[1]="x"
Solution:
Strings are immutable. So,new values cannot be assigned at any index position in a string.
Strings are immutable. So,new values cannot be assigned at any index position in a string.
Question: 3 -
What will be the output of below Python code?str1="Aplication"
str2=str1.replace('a','A')
print(str2)
-
Application
-
applicAtion
-
application
-
ApplicAtion
Answer:
ApplicAtion
Solution:
replace() function in string is used here to replace all the existing "a" by "A" in the given string.
replace() function in string is used here to replace all the existing "a" by "A" in the given string.
Question: 4 -
Which of the following is False?
-
lower() function in string is used to return a string by converting the whole given string into lowercase.
-
String is immutable.
-
None of these.
-
capitalize() function in string is used to return a string by converting the whole given string into uppercase.
Answer:
capitalize() function in string is used to return a string by converting the whole given string into uppercase.
Solution not available.
Question: 5 -
What will be the output of above Python code?str1="6/4"
print("str1")
-
str1
-
1
-
1.5
-
6/4
Answer:
str1
Solution:
Since in print statement, str1 is written inside double quotes so it will simply print str1 directly.
Since in print statement, str1 is written inside double quotes so it will simply print str1 directly.