Python Strings MCQs

1 min read

In Python, a string (str) is an immutable sequence of Unicode characters used to represent text data. Defined using single, double, or triple quotes, strings support sequence operations like positive and negative indexing, advanced slicing ([start:stop:step]), concatenation (+), and repetition (*). Because strings are immutable, attempting to modify a string character in place raises a TypeError. Python provides a comprehensive suite of built in string methods including .split(), .join(), .strip(), .replace(), .find(), and .startswith() as well as string formatting techniques like f-strings and .format(). Mastering Python strings is essential for text parsing, data cleaning, web scraping, and passing technical coding assessments.

1. Which of the following describes the internal storage of strings in Python 3?

a) Always ASCII encoded
b) Always UTF-8 encoded
c) Unicode code points
d) Null-terminated byte arrays
Correct Answer: c) Unicode code points
Explanation:
Python 3 strings are sequences of Unicode code points (UCS-2 or UCS-4 depending on the build), abstracting away the physical byte encoding.

2. What is the output ofprint(bool(""))?

a) True
b) False
c) None
d) Error
Correct Answer: b) False
Explanation:
An empty string in Python evaluates to False in a boolean context, as it is considered falsy.

3. How do you create a raw string in Python to treat backslashes as literal characters?

a) Prefix the string with 'r' or 'R'
b) Prefix the string with 'u' or 'U'
c) Prefix the string with 'b' or 'B'
d) Wrap the string inside double quotes with a backslash escape
Correct Answer: a) Prefix the string with 'r' or 'R'
Explanation:
Raw strings are prefixed with 'r' or 'R', which disables backslash escape sequences such as \n or \t.

4. What is the result of 'abc' + 'def'?

a) abcdef
b) abc def
c) Error
d) 3
Correct Answer: a) abcdef
Explanation:
The addition operator (+) performs string concatenation, joining two or more strings together without extra spaces.

5. Which method checks if all characters in a string are alphanumeric?

a) isalpha()
b) isalnum()
c) isdigit()
d) isnumeric()
Correct Answer: b) isalnum()
Explanation:
The isalnum() method returns True if all characters in the string are alphanumeric (either letters or numbers) and there is at least one character.

6. What does the expression "Python"[-1] evaluate to?

a) P
b) n
c) o
d) IndexError
Correct Answer: b) n
Explanation:
Negative indexing in Python counts from the right end of the sequence, where -1 represents the last character, which is 'n'.

7. Which method is used to split a string into a list of substrings based on a delimiter?

a) partition()
b) slice()
c) split()
d) join()
Correct Answer: c) split()
Explanation:
The split() method splits a string into a list using a specified separator (defaulting to any whitespace).

8. What is the output of "Hello".find("l")?

a) 2
b) 3
c) -1
d) 1
Correct Answer: a) 2
Explanation:
The find() method returns the lowest index where the substring is found. In 'Hello', the first 'l' appears at index 2.

9. What is the return type of the encode() method called on a string?

a) str
b) bytes
c) bytearray
d) list
Correct Answer: b) bytes
Explanation:
The encode() method returns a bytes object representing the string encoded in the specified encoding (defaulting to UTF-8).

10. Which format specifier centers a string within a field of a given width?

a)
c) ^
d) |
Correct Answer: c) ^
Explanation:
In Python's format mini-language, the caret character (^) specifies that the string should be centered within the available field width.

11. What happens if you try to modify a character in a string using indexing like s[0] = 'X'?

a) The string is modified in place
b) A new string is created with the modification
c) A TypeError is raised
d) A ValueError is raised
Correct Answer: c) A TypeError is raised
Explanation:
Because strings are immutable objects in Python, item assignment triggers a TypeError.

12. What is the output of " hello ".strip()?

a) "hello"
b) " hello"
c) "hello "
d) " hello "
Correct Answer: a) "hello"
Explanation:
The strip() method removes both leading and trailing whitespace characters from the string.

13. Which of the following functions converts an integer to its corresponding character?

a) ord()
b) chr()
c) str()
d) ascii()
Correct Answer: b) chr()
Explanation:
The chr() function takes an integer (Unicode code point) and returns the string representing that character. ord() does the reverse.

14. What does "Python".startswith("Py") return?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
The startswith() method checks if the string begins with the specified prefix, returning True in this case.

15. What is the result of "12345".isdigit()?

a) True
b) False
c) None
d) TypeError
Correct Answer: a) True
Explanation:
isdigit() returns True if all characters in the string are digits and there is at least one character.

16. How do you insert variables inside a string using f-strings?

a) f"Value is {variable}"
b) "Value is $variable"
c) "Value is %s" % variable
d) "Value is {}".format(variable)
Correct Answer: a) f"Value is {variable}"
Explanation:
Formatted string literals (f-strings) prefix the string with 'f' or 'F' and embed expressions inside curly braces.

17. What does "abc".zfill(5) return?

a) "00abc"
b) "abc00"
c) " abc"
d) "abc "
Correct Answer: a) "00abc"
Explanation:
The zfill() method pads the string on the left with ASCII '0' digits to reach the specified width.

18. What is the output of "apple".replace("p", "z")?

a) "azzle"
b) "azle"
c) "apple"
d) "p"
Correct Answer: a) "azzle"
Explanation:
The replace() method replaces all occurrences of the substring 'p' with 'z', transforming 'apple' into 'azzle'.

19. What is the output of len("\n")?

a) 2
b) 1
c) 0
d) Error
Correct Answer: b) 1
Explanation:
The newline character '\n' is treated as a single escape sequence character, so the length of the string containing only '\n' is 1.

20. Which method returns the number of non-overlapping occurrences of a substring?

a) find()
b) index()
c) count()
d) search()
Correct Answer: c) count()
Explanation:
The count() method returns the number of times a specified substring occurs within the given range or string.

21. What does "hello world".title() return?

a) "Hello World"
b) "HELLO WORLD"
c) "Hello world"
d) "hELLO wORLD"
Correct Answer: a) "Hello World"
Explanation:
The title() method capitalizes the first letter of every word in the string.

22. What will "Python"[:: -1] evaluate to?

a) Python
b) nohtyP
c) Ptohn
d) Error
Correct Answer: b) nohtyP
Explanation:
A step of -1 in string slicing reverses the entire string sequence.

23. Which operator is used for membership testing within strings?

a) in
b) contains
c) has
d) exists
Correct Answer: a) in
Explanation:
The 'in' operator checks if a substring exists within a string (e.g., 'Py' in 'Python').

24. What is the output of ",".join(["a", "b", "c"])?

a) "a,b,c"
b) "abc,"
c) "a b c"
d) Error
Correct Answer: a) "a,b,c"
Explanation:
The join() method concatenates the elements of an iterable (like a list of strings) using the string it is called on as a separator.

25. What does "Python".partition("th") return?

a) ("Py", "th", "on")
b) ["Py", "on"]
c) "Python"
d) Error
Correct Answer: a) ("Py", "th", "on")
Explanation:
partition() searches for the separator and returns a 3-tuple containing the part before, the separator itself, and the part after.

26. What is the output of "test".isupper()?

a) True
b) False
c) None
d) Error
Correct Answer: b) False
Explanation:
isupper() checks if all cased characters in the string are uppercase. Since 'test' is lowercase, it returns False.

27. What does the expression bool("False") return in Python?

a) False
b) True
c) None
d) ValueError
Correct Answer: b) True
Explanation:
Any non-empty string in Python, even one containing the text "False", evaluates to True when converted to a boolean.

28. Which method checks if a string ends with a specific suffix?

a) endswith()
b) finishswith()
c) last()
d) tail()
Correct Answer: a) endswith()
Explanation:
The endswith() method returns True if the string ends with the specified suffix, otherwise False.

29. What does "a\tb".expandtabs(4) do?

a) Replaces tabs with 4 spaces
b) Removes all tabs
c) Expands tab to maximum length
d) Raises an error
Correct Answer: a) Replaces tabs with 4 spaces
Explanation:
expandtabs() replaces tab characters ('\t') in the string with spaces, using the specified tab size (default is 8).

30. What is the result of "abc" == "ABC"?

a) True
b) False
c) None
d) Error
Correct Answer: b) False
Explanation:
String comparisons in Python are case-sensitive. Lowercase 'abc' is not equal to uppercase 'ABC'.

31. Which method converts a string to lowercase according to case-folding rules for caseless matching?

a) lower()
b) casefold()
c) swapcase()
d) normalize()
Correct Answer: b) casefold()
Explanation:
The casefold() method is stronger than lower() and is used for aggressive case-insensitive string comparisons.

32. What is the output of "Python".ljust(10, "*")?

a) "Python****"
b) "****Python"
c) "Python "
d) "**Python**"
Correct Answer: a) "Python****"
Explanation:
ljust() left-aligns the string in a string of length 10, padding the right side with the specified fill character '*'.

33. What is the result of "abc" < "abd"?

a) True
b) False
c) None
d) TypeError
Correct Answer: a) True
Explanation:
Strings are compared lexicographically (character by character based on Unicode code points). 'c' comes before 'd'.

34. Which escape sequence represents a carriage return?

a) \n
b) \t
c) \r
d) \b
Correct Answer: c) \r
Explanation:
\r is the escape sequence for a carriage return, while \n represents a newline.

35. What does "".join([]) return?

a) ""
b) None
c) []
d) Error
Correct Answer: a) ""
Explanation:
Joining an empty iterable of strings returns an empty string.

36. What happens if the start index is greater than the end index in a slice like "Python"[4:1]?

a) It returns an empty string
b) It raises a ValueError
c) It automatically reverses the string
d) It raises an IndexError
Correct Answer: a) It returns an empty string
Explanation:
When slicing with a positive step, if the start index is greater than or equal to the end index, Python returns an empty string rather than throwing an error.

37. Which module in Python provides advanced string formatting and manipulation utilities like templates?

a) string
b) textwrap
c) re
d) io
Correct Answer: a) string
Explanation:
The built-in 'string' module contains constants like string.ascii_letters and classes like string.Template.

38. What is the output of "hello".capitalize()?

a) "Hello"
b) "HELLO"
c) "hELLO"
d) "Hello world"
Correct Answer: a) "Hello"
Explanation:
The capitalize() method returns a copy of the string with its first character capitalized and the rest lowercased.

39. What does the expression ord('A') return?

a) 65
b) 97
c) 48
d) 10
Correct Answer: a) 65
Explanation:
The ord() function returns the integer Unicode code point of a given character. For uppercase 'A', it is 65.

40. What is the output of "abc123".isalnum()?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
isalnum() returns True because all characters in 'abc123' are either letters or numbers.

41. What does " ".isspace() return?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
isspace() returns True if there are only whitespace characters in the string and there is at least one character.

42. Which method splits a string at line breaks and returns a list of lines?

a) splitlines()
b) split("\n")
c) lines()
d) breaklines()
Correct Answer: a) splitlines()
Explanation:
The splitlines() method splits a string at universal newlines and returns a list of the lines.

43. What is the output of f"{2 + 3}"?

a) "5"
b) "2 + 3"
c) "7"
d) Error
Correct Answer: a) "5"
Explanation:
Inside an f-string expression braces, expressions are evaluated at runtime, resulting in "5".

44. What does "Python".index("y") return?

a) 1
b) 0
c) 2
d) ValueError
Correct Answer: a) 1
Explanation:
The index() method returns the index of the first occurrence of the substring. In 'Python', 'y' is at index 1.

45. What happens if a substring is not found when using the index() method?

a) It returns -1
b) It raises a ValueError
c) It raises a KeyError
d) It returns None
Correct Answer: b) It raises a ValueError
Explanation:
Unlike find(), which returns -1 when a substring is missing, index() raises a ValueError.

46. What is the result of "Python"[10:15]?

a) ""
b) IndexError
c) "Python"
d) None
Correct Answer: a) ""
Explanation:
Slicing indices out of bounds do not throw errors in Python; instead, they are gracefully truncated, resulting in an empty string.

47. Which method removes trailing characters from a string?

a) rstrip()
b) lstrip()
c) strip()
d) trim()
Correct Answer: a) rstrip()
Explanation:
The rstrip() method removes trailing characters (whitespace by default) from the right end of the string.

48. What does "python".swapcase() return?

a) "PYTHON"
b) "python"
c) "PYTHON" with inverted cases
d) "pYTHON"
Correct Answer: a) "PYTHON"
Explanation:
swapcase() converts all uppercase characters to lowercase and all lowercase characters to uppercase. For lowercase 'python', it returns 'PYTHON'.

49. What is the output of "a b c".split()?

a) ["a", "b", "c"]
b) ["a", " ", "b", " ", "c"]
c) "abc"
d) Error
Correct Answer: a) ["a", "b", "c"]
Explanation:
When split() is called without arguments, it splits by any whitespace and discards consecutive whitespace blocks.

50. Which formatting option pads a numeric string with zeros up to a given width?

a) zfill()
b) padzero()
c) zeropad()
d) fillzero()
Correct Answer: a) zfill()
Explanation:
zfill() pads strings on the left with zeros to match a specified width.

51. What is the output of len("abc\r\n")?

a) 3
b) 4
c) 5
d) 6
Correct Answer: c) 5
Explanation:
'abc' counts as 3, '\r' as 1, and '\n' as 1, totaling 5 characters.

52. What does "abc".rfind("b") return?

a) 1
b) 0
c) 2
d) -1
Correct Answer: a) 1
Explanation:
rfind() searches for the highest index where the substring is found, returning 1 for 'b' in 'abc'.

53. Which operator repeats a string multiple times in Python?

a) *
b) +
c) **
d) %
Correct Answer: a) *
Explanation:
The multiplication operator (*) performs string repetition when combined with an integer.

54. What is the output of "hello".istitle()?

a) True
b) False
c) None
d) Error
Correct Answer: b) False
Explanation:
istitle() returns True if the string is titlecased (words start with an uppercase character followed by lowercase). 'hello' is all lowercase, so it returns False.

55. What does "123".isnumeric() return?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
isnumeric() returns True if all characters in the string are numeric characters (including digits, fractions, roman numerals, etc.).

56. Which of the following creates a multiline string in Python?

a) Triple quotes (""" or ''')
b) Backslash continuation (\)
c) Semicolons (;)
d) Concatenation operator (+)
Correct Answer: a) Triple quotes (""" or ''')
Explanation:
Triple quotes allow strings to span multiple lines naturally without requiring explicit newline escape sequences.

57. What is the output of "Python".translate(str.maketrans("P", "J"))?

a) "Jython"
b) "Python"
c) "Pytjon"
d) Error
Correct Answer: a) "Jython"
Explanation:
The translate() method maps characters using a translation table created by str.maketrans(), replacing 'P' with 'J'.

58. What does "abc".center(7, "-") return?

a) "--abc--"
b) "---abc-"
c) "abc----
d) "----abc"
Correct Answer: a) "--abc--"
Explanation:
center() pads the string with the specified fill character '-' on both sides to achieve a total width of 7.

59. What is the output of "a,b,c".rsplit(",", 1)?

a) ["a,b", "c"]
b) ["a", "b,c"]
c) ["a", "b", "c"]
d) Error
Correct Answer: a) ["a,b", "c"]
Explanation:
rsplit() splits the string starting from the right. With maxsplit=1, it performs a single split from the right end.

60. Which method checks if a string consists entirely of lowercase letters?

a) islower()
b) islowerall()
c) lowercheck()
d) haslower()
Correct Answer: a) islower()
Explanation:
islower() returns True if all cased characters in the string are lowercase and there is at least one cased character.

61. What is the result of "abc".encode("ascii")?

a) b'abc'
b) "abc"
c) bytearray(b'abc')
d) Error
Correct Answer: a) b'abc'
Explanation:
Encoding a string returns a bytes literal object, denoted with a leading 'b'.

62. What does bytes.decode("utf-8") do?

a) Converts bytes back to a string
b) Converts a string to bytes
c) Encrypts the byte sequence
d) Compresses the data
Correct Answer: a) Converts bytes back to a string
Explanation:
The decode() method on bytes objects decodes the byte sequence into a standard Python string using the specified encoding.

63. What is the output of "test".removeprefix("te")?

a) "st"
b) "tes"
c) "test"
d) Error
Correct Answer: a) "st"
Explanation:
Introduced in Python 3.9, removeprefix() removes the specified prefix string if present, returning 'st'.

64. What does "test".removesuffix("st") return?

a) "te"
b) "tes"
c) "test"
d) Error
Correct Answer: a) "te"
Explanation:
Introduced in Python 3.9, removesuffix() removes the specified suffix string if present, returning 'te'.

65. What is the output of format(123, "05d")?

a) "00123"
b) "12300"
c) " 123"
d) Error
Correct Answer: a) "00123"
Explanation:
The format specifier '05d' formats an integer as a decimal zero-padded to a width of 5 characters.

66. Which of the following is true regarding string interning in Python?

a) All strings are automatically interned
b) Only strings resembling identifiers are automatically interned
c) Strings can never be interned
d) Interning only applies to bytes objects
Correct Answer: b> Only strings resembling identifiers are automatically interned
Explanation:
Python automatically interns strings that look like valid identifiers (e.g., variable names) for optimization, and sys.intern() can be used explicitly.

67. What is the output of "abc".rindex("b")?

a) 1
b) 0
c) 2
d) ValueError
Correct Answer: a) 1
Explanation:
rindex() returns the highest index where the substring is found, raising a ValueError if it is not present.

68. What does "Python".partition("x") return?

a) ("Python", "", "")
b) ("", "", "Python")
c) ValueError
d) None
Correct Answer: a) ("Python", "", "")
Explanation:
If the partition separator is not found, partition() returns the full string followed by two empty strings.

69. What is the output of "abc".ljust(5)?

a) "abc "
b) " abc"
c) "abc00"
d) "00abc"
Correct Answer: a) "abc "
Explanation:
ljust() pads the string on the right with spaces to fill the total width of 5.

70. Which function allows formatting values using a dictionary mapping through named placeholders?

a) str.format_map()
b) str.format()
c) str.map()
d) str.template()
Correct Answer: a) str.format_map()
Explanation:
format_map() works like format() but takes a mapping (such as a dictionary) directly without unpacking.

71. What is the output of bool(" ")?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
A string containing a space character is non-empty, so it evaluates to True in Python.

72. What does "HELLO".isupper() return?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
isupper() checks if all cased characters in the string are uppercase, returning True for 'HELLO'.

73. What is the result of "abc" * 0?

a) ""
b) "abc"
c) None
d) Error
Correct Answer: a) ""
Explanation:
Multiplying a string by 0 or a negative integer results in an empty string.

74. Which method checks if a string is a valid identifier in Python?

a) isidentifier()
b) isvariable()
c) isname()
d) checkidentifier()
Correct Answer: a) isidentifier()
Explanation:
isidentifier() returns True if the string is a valid identifier according to Python language definitions.

75. What does "abc".rpartition("b") return?

a) ("a", "b", "c")
b) ("ab", "b", "c")
c) ("a", "", "bc")
d) Error
Correct Answer: a) ("a", "b", "c")
Explanation:
rpartition() splits the string around the last occurrence of the separator, returning a 3-tuple.

76. What is the output of " python ".lstrip()?

a) "python "
b) " python"
c) "python"
d) " python "
Correct Answer: a) "python "
Explanation:
lstrip() removes leading whitespace characters from the left end of the string.

77. What does the expression f"{10:0b}" output?

a) "1010"
b) "0b1010"
c) "10"
d) "10100"
Correct Answer: b) "0b1010"
Explanation:
The format specifier '0b' formats an integer as a binary number with the '0b' prefix.

78. Which method returns a copy of the string with uppercase characters converted to lowercase and vice versa?

a) swapcase()
b) invertcase()
c) togglecase()
d) caseflip()
Correct Answer: a) swapcase()
Explanation:
swapcase() inverts the case of all cased characters in the string.

79. What is the output of len("abc\x00def")?

a) 7
b) 6
c) 3
d) Error
Correct Answer: a) 7
Explanation:
Python strings can contain embedded null bytes ('\x00'), which count as valid characters, making the length 7.

80. What does "abc".split("d") return?

a) ["abc"]
b) ["a", "b", "c"]
c) []
d) Error
Correct Answer: a) ["abc"]
Explanation:
If the split separator is not found in the string, split() returns a list containing the original string as its sole element.

81. Which option represents the correct way to include a literal brace character inside an f-string?

a) Double the braces ({{ or }})
b) Escape with backslash (\{ or \})
c) Use HTML entity codes
d) Literal braces are not allowed
Correct Answer: a) Double the braces ({{ or }})
Explanation:
To display a literal brace character inside an f-string, you must escape it by doubling it: {{ or }}.

82. What is the result of "Python"[::2]?

a) "Pto"
b) "yhn"
c) "Pyth"
d) "non"
Correct Answer: a) "Pto"
Explanation:
A slice with a step of 2 picks every second character starting from index 0 ('P', 't', 'o').

83. What does "abc".isalpha() return?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
isalpha() returns True if all characters in the string are alphabetic and there is at least one character.

84. What is the output of "12.34".isdecimal()?

a) False
b) True
c) None
d) Error
Correct Answer: a) False
Explanation:
isdecimal() returns True only if all characters are decimal characters and there is at least one character. The dot '.' is not a decimal character.

85. Which method fills a string with zeros on the left until it reaches a specified width?

a) zfill()
b) ljust()
c) rjust()
d) padzero()
Correct Answer: a) zfill()
Explanation:
zfill() pads the string on the left with ASCII '0' digits.

86. What is the output of "abc" in "abcdef"?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
The 'in' operator checks substring membership, returning True because 'abc' is present at the beginning of 'abcdef'.

87. What does "abc".upper() return?

a) "ABC"
b) "abc"
c) "Abc"
d) Error
Correct Answer: a) "ABC"
Explanation:
The upper() method converts all lowercase characters in the string to uppercase.

88. What is the output of "hello".find("x")?

a) -1
b) 0
c) ValueError
d) None
Correct Answer: a) -1
Explanation:
If the substring is not found using find(), Python returns -1 rather than raising an exception.

89. Which method translates characters according to a translation table, deleting characters specified in a delete table?

a) translate()
b) replace()
c) sub()
d) map()
Correct Answer: a) translate()
Explanation:
The translate() method can take a translation table and an optional deletechars argument to remove characters.

90. What is the output of f"{1234567:,}"?

a) "1,234,567"
b) "1,234,567,"
c) "1234,567"
d) Error
Correct Answer: a) "1,234,567"
Explanation:
The comma specifier (,) in format strings uses commas as thousands separators.

91. What does "python".capitalize() do to the first character?

a) Converts it to uppercase
b) Converts it to lowercase
c) Deletes it
d) Duplicates it
Correct Answer: a) Converts it to uppercase
Explanation:
capitalize() transforms the first character of the string to uppercase and ensures the remaining characters are lowercase.

92. What is the result of "abc" + 5?

a) TypeError
b) "abc5"
c) "abcabcabcabcabc"
d) ValueError
Correct Answer: a) TypeError
Explanation:
Python does not implicitly convert integers to strings during addition (+), raising a TypeError. String repetition requires the multiplication operator (*).

93. Which built-in module contains regular expression support frequently used with complex string matching?

a) re
b) string
c) textwrap
d) regex
Correct Answer: a) re
Explanation:
The 're' module is Python's built-in library for regular expression pattern matching and string searching.

94. What is the output of "a\nb".splitlines()?

a) ["a", "b"]
b) ["a\nb"]
c) "ab"
d) Error
Correct Answer: a) ["a", "b"]
Explanation:
splitlines() breaks the string at newline boundaries, returning a list of individual lines.

95. What does "abc".rjust(5, "0") return?

a) "00abc"
b) "abc00"
c) " abc"
d) "abc "
Correct Answer: a) "00abc"
Explanation:
rjust() right-aligns the string to a width of 5, filling the left side with the character '0'.

96. What is the output of bool("None")?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
"None" is a non-empty string containing characters, so it evaluates to True (unlike the actual NoneType object, which is falsy).

97. Which method is used to format strings by substituting placeholders with positional or keyword arguments?

a) format()
b) substitute()
c) render()
d) template()
Correct Answer: a) format()
Explanation:
The str.format() method allows robust string formatting using curly braces as placeholders.

98. What does "Python".casefold() produce?

a) "python"
b) "PYTHON"
c) "Python"
d) Error
Correct Answer: a) "python"
Explanation:
casefold() returns a lowercased version of the string suitable for caseless comparisons.

99. What is the output of f"{3.14159:.2f}"?

a) "3.14"
b) "3.141"
c) "3.15"
d) "3.142"
Correct Answer: a) "3.14"
Explanation:
The format specifier '.2f' formats a floating-point number fixed to two decimal places.

100. What happens when you slice a string with a step of 0, such as "Python"[::0]?

a) Raises a ValueError
b) Returns an empty string
c) Returns the original string
d) Raises a ZeroDivisionError
Correct Answer: a) Raises a ValueError
Explanation:
Slice step values cannot be zero in Python; attempting to use a step of 0 raises a ValueError.

101. What does "abc".count("a", 1, 3) return?

a) 0
b) 1
c) 2
d) ValueError
Correct Answer: a) 0
Explanation:
The count() method can accept optional start and end slice parameters. In 'abc', searching for 'a' between indices 1 and 3 yields 0 occurrences because 'a' is at index 0.

102. Which built-in function returns the string representation of any arbitrary Python object?

a) str()
b) repr()
c) ascii()
d) format()
Correct Answer: a) str()
Explanation:
The str() constructor or built-in function returns an informal or printable string representation of an object.
← Previous: Python OOP MCQs
Next →: Python Variables & Data Types MCQs
NewTop Python Fundamentals MCQs & Answers for Beginners

Top Python Fundamentals MCQs & Answers for Beginners

Python is a dynamically typed, high-level programming language created by Guido van Rossum in 1991. Renowned for its clear syntax…

By MCQs Generator
NewPython Control Flow MCQs

Python Control Flow MCQs

Control flow statements regulate the order in which individual code statements are evaluated and executed in a Python program. Python…

By MCQs Generator
NewLatest Python Loops MCQs

Latest Python Loops MCQs

Loops in Python provide the fundamental mechanism for executing a block of code repeatedly until a specified condition is met…

By MCQs Generator