Python String MCQ
This section focuses on the "String" in Python. These Multiple Choice Questions (mcq) should be practiced to improve the Python skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. What will be the output of above Python code?
Explanation: Since in print statement, str1 is written inside double quotes so it will simply print str1 directly.
2. Which of the following will result in an error?
Explanation: Strings are immutable. So,new values cannot be assigned at any index position in a string
3. Which of the following is False?
Explanation: capitalize() function in string gives the output by converting only the first character of the string into uppercase and rest characters into lowercase.However, upper() function is used to return the whole string into uppercase.
4. What will be the output of below Python code?
Explanation: 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".
5. What will be the output of below Python code?
Explanation: replace() function in string is used here to replace all the existing "a" by "A" in the given string.
6. What will be the output of below Python code?
Explanation: str1.upper() returns the uppercase of whole string str1. However,it doesnot change the string str1. So, output will be the original str1.
7. What will the below Python code will return?
If str1="save paper,save plants"
Explanation: It returns the first index position of the first occurance of "save" in the given string str1.
8. What will the below Python code will return?
for i in list1:
Explanation: 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.
9. Which of the following will give "Simon" as output?
Explanation: 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.
10. What will following Python code return?
str1="Stack of books"
Explanation: len() returns the length of the given string str1, including spaces and considering " " as a single character.