This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Strings – 9”.
1. What will be the output of the following Python code?
print('{0:.2}'.format(1/3))
a) 0.333333
b) 0.33
c) 0.333333:.2
d) Error
View Answer
Explanation: The format specifier {0:.2} is used to format the first argument (1/3) to a precision of 2 significant digits. In this case, 1/3 ≈ 0.3333, and when rounded to 2 significant digits, it becomes 0.33. So, the output is 0.33.
2. What will be the output of the following Python code?
print('{0:.2%}'.format(1/3))
a) 0.33
b) 0.33%
c) 33.33%
d) 33%
View Answer
Explanation: The format specifier {0:.2%} converts the number to a percentage by multiplying it by 100 and appending a % sign. It also rounds the result to 2 decimal places. Since 1/3 ≈ 0.3333, it becomes 33.33% when formatted this way.
3. What will be the output of the following Python code?
print('ab12'.isalnum())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The .isalnum() method returns True if the string contains only alphanumeric characters (letters and digits) and no spaces or special characters. Since ‘ab12’ contains only letters and digits, the result is True.
4. What will be the output of the following Python code?
print('ab,12'.isalnum())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The .isalnum() method returns True only if all characters in the string are letters or digits. In ‘ab,12’, the comma , is neither a letter nor a digit, so .isalnum() returns False.
5. What will be the output of the following Python code?
print('ab'.isalpha())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The isalpha() method in Python returns True if all characters in the string are alphabetic (i.e., letters from A–Z or a–z) and there is at least one character. In this case, ‘ab’ contains only letters, so the result is True.
6. What will be the output of the following Python code?
print('a B'.isalpha())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The isalpha() method returns True only if all characters in the string are alphabetic and there is at least one character. In the string ‘a B’, there is a space character, which is not alphabetic. Hence, ‘a B’.isalpha() returns False.
7. What will be the output of the following Python code snippet?
print('0xa'.isdigit())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The .isdigit() method returns True only if all characters in the string are decimal digits (0–9). In ‘0xa’, the characters ‘x’ and ‘a’ are not digits, so the method returns False. Hexadecimal representations like ‘0xa’ are not treated as numeric digits by .isdigit().
8. What will be the output of the following Python code snippet?
print(''.isdigit())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The .isdigit() method returns True only if all characters in the string are digits and there is at least one character. Since the string is empty (”), there are no characters to check, so .isdigit() returns False.
9. What will be the output of the following Python code snippet?
print('my_string'.isidentifier())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The .isidentifier() method returns True if the string is a valid Python identifier, meaning it can be used as a variable name. ‘my_string’ follows the rules: it starts with a letter, contains only letters, digits, or underscores, and doesn’t use any reserved keywords. So, the result is True.
10. What will be the output of the following Python code snippet?
print('__foo__'.isidentifier())
a) True
b) False
c) None
d) Error
View Answer
Explanation: The .isidentifier() method in Python checks whether a string is a valid identifier. A string like ‘__foo__’ is considered valid because it starts with an underscore and contains only alphanumeric characters or underscores. Special names like this are often used internally in Python but are still valid identifiers.
Sanfoundry Global Education & Learning Series – Python.
To practice all mcqs on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Python Internship
- Check Information Technology Books
- Check Python Books
- Apply for Programming Internship
- Practice Programming MCQs