Strings are an essential data type in almost every programming language, including Python. They are used to represent and manipulate sequences of characters. In this page, we’ve curated a set of 20+ Multiple Choice Questions (MCQs) tailored for beginners to test and strengthen their knowledge on Python strings.
Each question is followed by a concise explanation to clarify the underlying concept. Let’s delve in!
1. What is the data type of: ‘Hello, World!’?
Answer:
Explanation:
Anything enclosed in single (‘ ‘) or double (” “) quotes in Python is a string.
2. Which of the following is the correct way to get the length of a string?
Answer:
Explanation:
The built-in len() function returns the length of the provided string.
3. Which of these will throw an error?
Answer:
Explanation:
Strings in Python support concatenation using + and repetition using *, but not subtraction or division.
4. How would you extract ‘World’ from the string ‘Hello, World!’?
Answer:
Explanation:
String slicing begins at the start index and goes up to, but does not include the end index.
5. What will the following code return? ‘Python’.lower()
Answer:
Explanation:
The .lower() method converts all the characters in a string to lowercase.
6. Which method can be used to check if the string starts with a specified value?
Answer:
Explanation:
The startswith() method returns True if a string starts with the specified value, otherwise False.
7. How can you replace all occurrences of ‘good’ with ‘bad’ in the string ‘Life is good’?
Answer:
Explanation:
The replace() method replaces a specified value with another value in a string.
8. Which string method can be used to remove any leading and trailing whitespace?
Answer:
Explanation:
The strip() method removes leading and trailing whitespaces from a string.
9. Which of the following strings is a palindrome?
Answer:
Explanation:
A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward.
10. What is the output of ‘Python’.isalpha()?
Answer:
Explanation:
The isalpha() method returns True if all characters in the string are alphabetic, otherwise, it is False.
11. How can you convert a string to a list of words?
Answer:
Explanation:
The split() method divides a string into a list, based on a specified delimiter (default is space).
12. Which of the following will return the string ‘Python’ backward?
Answer:
Explanation:
String slicing with [::-1] returns the string in reverse order.
13. Which method will return the position of the first occurrence of ‘o’ in ‘Hello’?
Answer:
Explanation:
The index() method returns the position at the first occurrence of the specified value.
14. What does ‘ ‘.isspace() return?
Answer:
Explanation:
The isspace() method returns True if all characters in a string are whitespaces, otherwise False.
15. Which of the following is correct for checking if a string ‘s’ is both alphanumeric and has at least one character?
Answer:
Explanation:
The isalnum() method checks if all characters in a string are alphanumeric. Coupling this check with a length check ensures that the string has at least one character.
16. What does the str.upper() method do?
Answer:
Explanation:
The str.upper() method converts all characters in the string to uppercase.
17. Which of these methods removes only trailing whitespace?
Answer:
Explanation:
The str.rstrip() method removes trailing whitespaces from a string.
18. What will ‘123’.isdigit() return?
Answer:
Explanation:
The isdigit() method checks if all the characters in a string are digits and returns True if they are, otherwise, it is False.
19. How can you find the occurrence of a substring within a string?
Answer:
Explanation:
Both str.find() and str.index() can be used to find the occurrence of a substring within a string. The difference is that str.find() returns -1 if the substring is not found, whereas str.index() raises an exception.
20. Which method will replace a substring within a string?
Answer:
Explanation:
The str.replace() method replaces a specified value with another value in a string.
21. What will be the output of the method call ‘python’.capitalize()?
Answer:
Explanation:
The capitalize() method capitalizes the first letter of the string and makes the rest of the characters lowercase.
22. If you want to split a string ‘apple,banana,cherry’ into a list of fruits, which method would you use?
Answer:
Explanation:
The split() method divides a string into a list based on a specified delimiter. In this case, the comma is used as the delimiter.
23. To count the number of occurrences of the letter ‘a’ in the string ‘banana’, which method should you use?
Answer:
Explanation:
The count() method is used to return the number of times a specified value appears in the string.
Strings form the basis of data representation in many programs and applications. Whether you’re dealing with user inputs, file handling, or even web scraping, understanding strings is crucial. We hope these MCQs have provided a comprehensive review of strings in Python and have further solidified your understanding. Keep practicing and happy coding!