30 Python String Quiz – MCQ Questions

Welcome to this challenging Python String quiz! Here, we have collected 30 questions based on Python string which will cover basic to advanced concepts of string. This MCQ quiz will test your knowledge in string as well as level up your understanding in string.

Whether you’re a beginner or an experienced Python programmer, you should test your knowledge in string. So, are you ready to score 30/30? Let’s see, how well do you really understand Python string? Let’s begin with question number 1.👇

1. What is a Python string?
A. A sequence of integers
B. A sequence of Unicode characters
C. A list of bytes
D. A mutable data type
b
A Python string is an immutable sequence of Unicode characters (text data).
2. How are strings represented in Python?
A. Single quotes ‘ ‘ only
B. Double quotes ” ” only
C. Either single ‘ ‘ or double ” ” quotes
D. Backticks ` `
c
Python allows both single ‘ ‘ or double ” ” quotes. You can also use Triple quotes (”’ or “””) for multi-line strings.
3. What will be the output of the following code?
str = "Scientech, Easy!"
print(len(str))
A. 13
B. 14
C. 15
D. 16
d
The len() function in Python returns the number of characters in the string, including spaces and punctuation. “Scientech, Easy!” has 16 characters.
4. Which of the following statement is incorrect about string in Python?
A. String is an immutable object in Python.
B. A string is a contiguous set of characters.
C. There is no character data type in Python.
D. None of the above
d
All are correct statements in Python.
5. What is the output of print(“Python” * 3)?
A. PythonPythonPython
B. Python Python Python
C. [Python, Python, Python]
D. None
a
The * operator is used for string repetition in Python. “Python” * 3 repeats the string three times.
6. What is the output of this code?
s = "Python"
print(s[::-1])
A. Python
B. nohtyP
C. Pytho
D. Error
b
The slicing syntax [::-1] reverses the string by stepping backwards through all characters.
7. Which of these will not produce the string “Python 3”?
A. “Python ” + “3”
B. “Python ” + str(3)
C. “Python ” * 3
D. “Python ” + “{}”.format(3)
c
The statement “Python ” * 3 will produce “Python Python Python ” (repeats the string 3 times) rather than concatenating with “3”.
8. What will be the output of the following code?
s = "abcdef"
print(s[1:5:2])
print(s[0:3])
A. bcde ab
B. bd ab
C. bd abc
D. bcd abc
c
The slicing [1:5:2] starts at index 1 (b), stops before index 5 (e), and takes every second character (b and d). Next, the slicing [0:3] starts to slice string at position 0 and slices up to position 3 and returns the result.
9. What is the result of this expression?
print("a" in "apple" == True)
A. True
B. False
C. Error
D. None
b
Due to Python’s operator chaining, the expression “a” in “apple” == True is evaluated as (“a” in “apple”) and (“apple” == True) which is True and False. Therefore, the output is False.
10. What is the output of this code if no error?
s = "Python"
s[0] = "J"
print(s)
A. Jython
B. Python
C. J
D. TypeError
d
Strings are immutable in Python, so attempting to modify a character at a specific index raises a TypeError.
11. What will be the expected output of the following code?
s = "Python Programming"
print(s.find("o", 5, 9))
A. 4
B. 6
C. -1
D. 9
c
The find() method searches for “o” between index 5 and 9. Since “o” appears at index 4 and 9, but 9 is outside the search range, it returns -1.
12. What will be the expected result of the following code?
s = "abc" + str(1) + "def"
print(s)
A. abcdef
B. abc1def
C. ab1cdef
D. abc def
b
The str(1) converts the integer 1 to a string, and concatenation results in “abc1def”.
13. Which of the following output is correct for the program code?
s = "abcdef"
print(s[-3:])
print(s[-3:-1])
print(s[:-2])
A. def de abcd
B. ef de abc
C. def de abc
D. def de No output
a
The slicing s[-3:] starts from the third character from the end (d) and extracts the rest. Similarly, the slicing s[-3:-1] starts from the third character from the end (d) and slice up to the character (e) and returns the result. The slicing s[:-2] takes all the characters except the last two characters. It excludes the last 2 characters of the string (or sequence).
14. Which of these correctly checks if a string contains only digits?
A. s.isdigit()
B. s.isnumeric()
C. s.isdecimal()
D. All of the above
d
All three methods return True if all characters in the string are digits. Though, they have subtle differences with Unicode characters.
15. What is the expected output of the below Python code?
print("".join(["a", "b", "c"]) or "default")
A. abc
B. default
C. “”
D. [“a”, “b”, “c”]
a
The join() method joins the string in the sequence. It inserts a string between each character of the string sequence and returns the concatenated string. Therefore, the result is abc. Since it is a non-empty string (truthy), so the or returns this value without evaluating the second part.
16. What is the output of the following Python code?
s = "python"
print(s[-100:100])
print(s[-1:-1])
A. “python” and “n”
B. “” and “n”
C. “python” and “”
D. IndexError
c
Python string slicing never raises IndexError for out-of-range indices. If start >= end (unless using a negative step), the result is ” ” (empty string).
17. What will be the output of the given Python code?
s = "Python"
print(s[-1::-1])
print(s[-1:-3:-1])
A. nohtyP noh
B. nohtyP noht
C. nohty noh
D. nohtyP no
d
s[-1] refers to the last character of “Python”, which is “n”. The slicing s[-1::-1] means start at index -1 (which is “n”) and move backward (step = -1). Since no stopping index is specified, so it goes until the beginning of the string. This reverses the entire string. Next, the s[-1] refers to “n”, s[-3] refers to “h”, but in Python slicing, the stop index is exclusive. So, the s[-1:-3:-1] means start from index -1 (“n”), move backward (step = -1), and stop before index -3 (“t”). So, it includes characters from “n” to “o”, stopping before “t”.
18. What will be the expected output of the following Python code?
print("Scientech".replace("e", "z", 1))
A. Scizntzch
B. Scizntecz
C. Scizntzcz
D. Scizntech
d
The replace() method In Python replaces the first occurrence of “e” with “z” and the third argument (1) limits the number of replacements to one.
19. Which of the following output will be correct for the below Python code?
str = "Scientech Easy"
print(str.count("e"))
print(str.capitalize())
A. 2 Scientech Easy
B. 2 Scientech easy
C. 3 Scientech Easy
D. Error
b
The count() method in Python counts occurrences of a substring. In the above code, “e” appears twice in “Scientech Easy”, so the output is 2. Next, the capitalize() method converts the first character to uppercase and all other characters to lowercase. So, the result is Scientech easy.
20. Which of the following statements are correct in Python string?
  1. The find() method returns the index position of first occurrence of substring in a specified string.
  2. The isalnum() method checks a string contains alphanumeric characters (no space, and special characters).
  3. The strip() method removes all leading and trailing whitespace character from the start and end of a string, not inside it.
A. Only 1 and 2
B. Only 1
C. Only 2 and 3
D. 1, 2, and 3
d
All are the correct statements.
21. What will be the output of print(“Python”.swapcase())?
A. PYTHON
B. python
C. pYTHON
D. None
c
The swapcase() method in Python swaps the case of all characters converting from uppercase to lowercase and vice versa.
22. What does the following Python code output?
str = "Python Programming"
print(str.index("o", 5))
A. 5
B. 7
C. 9
D. 10
c
The index() method finds the first occurrence of “o” after index 5. The “o” in “Programming” appears at index 9.
23. What will be the output of the print(bool(“”))?
A. True
B. False
C. Error
D. None
b
An empty string evaluates to False in a boolean context.
24. What will the following Python code output?
s = "abc"
s += "def"
print(s)
A. abcdef
B. [“abc”, “def”]
C. abc def
D. None of the above
a
The += operator concatenates strings. “abc” + “def” results in “abcdef”.
25. If no error found in this Python code, what will be output?
s = "Python"
print(s[1:100])
print(s * 0)
A. ython “” (empty string)
B. ython100 Python
C. “” (empty string) “” (empty string)
D. TypeError
a
Python does not raise an error if the stop index is larger than the string length. It simply slices until the end of the string. The s[1:100] means start from index 1 (“y”) and go up to the end of the string. Thus, the output is “ython”. Next, the * operator repeats a string. The line “Python” * 0 means zero repetitions, so it returns an empty string.
26. What does the Python code output if no error found?
str = "Python 12.34"
print(str.isdigit())
print(str[::-2])
A. True nhy
B. False nhy
C. True 4.1nhy
D. False 4.1nhy
d
The isdigit() method in Python returns True only if all characters in the string are digits (0-9). The string “Python 1.23” contains a dot (.), which is not a digit. Therefore, isdigit() returns False. Next, the slicing s[::-2] means start from the end (“4”) and move backward with step -2. Thus, the output is “4.1nhy”.
27. What will be the output of the following Python code?
print("apple" > "banana")
print("python" > "python ")
A. True True
B. True False
C. False True
D. False False
d
String comparison in Python follows lexicographical (dictionary order). Here, “a” comes before “b”, so “apple” < "banana". Therefore, "apple" > “banana” is False. Next, the shorter strings come first in lexicographical order. Therefore, “python ” (with a space) is longer than “python”. “python” < "python ", so "python" > “python ” outputs False.
28. What will be the expected output of the string comparisons in the given program code?
print("10" > "2")
print("abcd" > "abCd")
A. True True
B. True False
C. False True
D. False False
c
Python uses ASCII values for character comparison in the strings. “10” starts with “1”, and “2” starts with “2”. Since “1” < "2", "10" < "2", so "10" > “2” is False. Similarly, the ASCII value of C character is 67, while the value of c character is 99. Therefore, “C” comes before “c”. “abcd” > “abCd”, which results True.
29. What does this code output?
print(" " < "A")
print("apple".strip() == "apple ".strip())
A. True True
B. True False
C. False True
D. False False
a
The space character (" ") has an ASCII value of 32. "A" has an ASCII value of 65. Since 32 < 65, " " < "A" is True. In Python, the strip() method removes any leading and trailing whitespace (spaces, tabs, newlines) from a string. However, it does not modify spaces inside the string. The first string "apple" has no spaces at the start or end, so strip() does nothing. The second string "apple " has a trailing space, which is removed by strip(). Since both strings are now exactly the same, the comparison returns True.
30. What does the following Python code output?
a = "Hello" "World"
b = "Hello" + "World"
print(a is b)
print(b == "HelloWorld")
A. True True
B. True False
C. False True
D. False False
a
Both will output True.

Quiz Results

0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score
You can do better next time!