Python MCQs Strings

Q1. Which data type is used to store text in Python?

A) char
B) string
C) str
D) text
✅ Correct Answer: (C) str
📘 Explanation: Python uses the str data type to store text.

Q2. Which of the following is the correct way to create a string?

A) ‘Hello’
B) Hello
C) str = Hello
D) text(Hello)
✅ Correct Answer: (A) ‘Hello’
📘 Explanation: Strings must be enclosed in quotes.

Q3. Which operator is used to concatenate strings?

A) *
B) +
C) –
D) /
✅ Correct Answer: (B) +
📘 Explanation: The + operator joins strings.

Q4. Which operator is used to repeat a string?

A) +
B) *
C) #
D) %
✅ Correct Answer: (B) *
📘 Explanation: The * operator repeats a string.

Q5. What is the output of the following code?

print("Python"[0])


A) P
B) y
C) n
D) Error
✅ Correct Answer: (A) P
📘 Explanation: Indexing starts from 0 in Python.

Q6. Which function returns the length of a string?

A) size()
B) length()
C) len()
D) count()
✅ Correct Answer: (C) len()
📘 Explanation: len() returns the number of characters.

Q7. What is the output of “Hello”[1:4]?

A) Hel
B) ell
C) Hello
D) lo
✅ Correct Answer: (B) ell
📘 Explanation: Slicing starts from index 1 up to (but not including) 4.

Q8. Which method converts a string to uppercase?

A) upper()
B) capitalize()
C) lower()
D) title()
✅ Correct Answer: (A) upper()
📘 Explanation: upper() converts all characters to uppercase.

Q9. Which method removes whitespace from both ends of a string?

A) trim()
B) strip()
C) clean()
D) remove()
✅ Correct Answer: (B) strip()
📘 Explanation: strip() removes leading and trailing spaces.

Q10. What will be the output of the following code?

print("Python".lower())


A) PYTHON
B) python
C) PyThoN
D) Error
✅ Correct Answer: (B) python
📘 Explanation: lower() converts all letters to lowercase.

Q11. Which method is used to replace a substring?

A) change()
B) replace()
C) swap()
D) modify()
✅ Correct Answer: (B) replace()
📘 Explanation: replace(old, new) replaces occurrences.

Q12. Which method splits a string into a list?

A) divide()
B) cut()
C) split()
D) break()
✅ Correct Answer: (C) split()
📘 Explanation: split() divides a string into list elements.

Q13. Which of the following is an immutable object?

A) list
B) set
C) string
D) dictionary
✅ Correct Answer: (C) string
📘 Explanation: Strings cannot be changed after creation.

Q14. What is the output of “abc” * 3?

A) abcabcabc
B) abc abc abc
C) Error
D) abc3
✅ Correct Answer: (A) abcabcabc
📘 Explanation: * repeats the string.

Q15. Which method checks if a string starts with a specific substring?

A) starts()
B) startwith()
C) startswith()
D) begin()
✅ Correct Answer: (C) startswith()
📘 Explanation: startswith() checks prefix.

Q16. Which method checks if all characters are digits?

A) isnum()
B) isdigit()
C) isnumeric()
D) isnumber()
✅ Correct Answer: (B) isdigit()
📘 Explanation: isdigit() returns True if all characters are digits.

Q17. What is the output of the following code?

print("Hello".find("e"))


A) 0
B) 1
C) -1
D) Error
✅ Correct Answer: (B) 1
📘 Explanation: find() returns the index of first occurrence.

Q18. Which method converts the first character to uppercase?

A) upper()
B) capitalize()
C) title()
D) swapcase(
✅ Correct Answer: (B) capitalize()
📘 Explanation: capitalize() capitalizes the first letter.

Q19. Which operator is used to check substring presence?

A) in
B) has
C) contains
D) is
✅ Correct Answer: (A) in
📘 Explanation: in checks membership in a string.

Q20. What is the output of the following code?

print("Python"[-1])


A) P
B) n
C) o
D) Error
✅ Correct Answer: (B) n
📘 Explanation: Negative indexing accesses elements from the end.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top