Python Strings Quiz - MCQ Questions and Answers

Welcome to our "Python Strings Quiz - MCQ Questions and Answers" blog post. This quiz is tailored for those looking to test and enhance their knowledge of Python strings, one of the most fundamental and widely used data types in Python programming. Whether you're a beginner seeking to solidify your string handling skills or an experienced developer looking for a quick refresher, this quiz covers a range of topics from basic string operations to more complex manipulations. Expect to encounter questions on string formatting, slicing, built-in methods, and much more. 

Each question is accompanied by a detailed explanation to not only test your knowledge but also expand it. Let's dive in and explore the intricacies of Python strings through this engaging and informative quiz!

1. How do you create a string in Python?

a) Using single or double quotes
b) With square brackets []
c) Using parentheses ()
d) With curly braces {}

Answer:

a) Using single or double quotes

Explanation:

Strings in Python are created by enclosing characters in single or double quotes.

2. What is the output of len("Python")?

a) 5
b) 6
c) Error
d) 7

Answer:

b) 6

Explanation:

len() function returns the length of the string, which is 6 in this case.

3. How do you access the first character of a string?

a) string[1]
b) string[0]
c) string.first()
d) first(string)

Answer:

b) string[0]

Explanation:

In Python, string indices start at 0.

4. What is slicing in Python?

a) Dividing a string into multiple parts
b) Changing a string
c) Removing a part of a string
d) None of the above

Answer:

a) Dividing a string into multiple parts

Explanation:

Slicing is used to extract a portion of a string.

5. How would you extract "Pyt" from "Python" using slicing?

a) "Python"[0:2]
b) "Python"[0:3]
c) "Python"[1:3]
d) "Python"[:3]

Answer:

b) "Python"[0:3]

Explanation:

Slicing with [0:3] extracts characters from index 0 to 2.

6. What does "Hello" + "World" return in Python?

a) HelloWorld
b) "HelloWorld"
c) Error
d) "Hello World"

Answer:

b) "HelloWorld"

Explanation:

The + operator concatenates strings without adding any space.

7. What does str(123) return?

a) 123
b) "123"
c) Error
d) None

Answer:

b) "123"

Explanation:

str() converts the number into a string.

8. How do you check if a string contains a particular substring?

a) "substring" in string
b) string.contains("substring")
c) contains(string, "substring")
d) string.find("substring")

Answer:

a) "substring" in string

Explanation:

The 'in' keyword is used to check if a string contains a particular substring.

9. What is the output of "python".capitalize()?

a) "Python"
b) "python"
c) "PYTHON"
d) "pYTHON"

Answer:

a) "Python"

Explanation:

The capitalize() method capitalizes the first letter of the string.

10. What is the result of 'Py' * 3 in Python?

a) "PyPyPy"
b) "Py3"
c) Error
d) "PPPyyy"

Answer:

a) "PyPyPy"

Explanation:

The * operator repeats the string the specified number of times.

11. What is the output of "PYTHON".lower()?

a) "PYTHON"
b) "python"
c) "Python"
d) "pYTHON"

Answer:

b) "python"

Explanation:

The lower() method converts all characters in a string to lowercase.

12. How do you replace "Python" with "Java" in the string "I love Python"?

a) "I love Python".replace("Python", "Java")
b) replace("I love Python", "Python", "Java")
c) "I love Python".replace("Java")
d) "I love Java"

Answer:

a) "I love Python".replace("Python", "Java")

Explanation:

The replace() method replaces a specified phrase with another specified phrase.

13. What is the output of '123'.isdigit()?

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

Answer:

a) True

Explanation:

isdigit() returns True if all characters in the string are digits.

14. What does ' '.join(['Python', 'is', 'awesome']) return?

a) "Pythonisawesome"
b) "Python is awesome"
c) ["Python", "is", "awesome"]
d) "Python, is, awesome"

Answer:

b) "Python is awesome"

Explanation:

join() method concatenates elements of the iterable with the specified string.

15. How do you get the ASCII value of a character in Python?

a) ascii(char)
b) char.ascii()
c) ord(char)
d) chr(char)

Answer:

c) ord(char)

Explanation:

ord() function returns the ASCII value of a character.

16. What is string immutability in Python?

a) Strings can be changed after they are created
b) Strings cannot be changed once they are created
c) Strings can be deleted
d) None of the above

Answer:

b) Strings cannot be changed once they are created

Explanation:

In Python, strings are immutable, meaning they cannot be changed after creation.

17. What is the output of "Python"[::-1]?

a) "Python"
b) "nohtyP"
c) "P"
d) Error

Answer:

b) "nohtyP"

Explanation:

Slicing with [::-1] reverses the string.

18. How do you concatenate variables a = "Python" and b = "Programming"?

a) a + b
b) concat(a, b)
c) a.concat(b)
d) a & b

Answer:

a) a + b

Explanation:

The + operator is used to concatenate strings.

19. What is the output of "Python".find("p")?

a) 0
b) 1
c) -1
d) Error

Answer:

c) -1

Explanation:

The find() method returns -1 if the substring is not found, and it is case-sensitive.

20. What does the strip() method do?

a) Removes all spaces in a string
b) Removes spaces at the beginning and end of a string
c) Deletes a string
d) Splits a string

Answer:

b) Removes spaces at the beginning and end of a string

Explanation:

strip() method removes any leading and trailing characters (space is the default leading character to remove).

Comments