Python MCQ – String – 9

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

Answer: b
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.
advertisement

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

Answer: c
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.
Free 30-Day Python Certification Bootcamp is Live. Join Now!

3. What will be the output of the following Python code?

print('ab12'.isalnum())

a) True
b) False
c) None
d) Error
View Answer

Answer: a
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.
advertisement

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

Answer: b
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

Answer: a
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

Answer: b
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

Answer: b
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

Answer: b
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

Answer: a
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

Answer: a
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.