Python String MCQ Questions and Answers

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 blog post, 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!'?

a) List
b) Tuple
c) Integer
d) String

Answer:

d) String

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?

a) len(string)
b) string.len()
c) string.length()
d) length(string)

Answer:

a) len(string)

Explanation:

The built-in len() function returns the length of the provided string.

3. Which of these will throw an error?

a) 'Hello' + ' World'
b) 'Hello' - 'o'
c) 'Hello' * 3
d) 'Hello' / 2

Answer:

b) 'Hello' - 'o'

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!'?

a) 'Hello, World!'[7:11]
b) 'Hello, World!'[6:11]
c) 'Hello, World!'[7:12]
d) 'Hello, World!'[6:12]

Answer:

d) 'Hello, World!'[6:12]

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()

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

Answer:

b) python

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?

a) begin()
b) start()
c) beginWith()
d) startswith()

Answer:

d) startswith()

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'?

a) 'Life is good'.replace('good', 'bad')
b) 'Life is good'.swap('good', 'bad')
c) 'Life is good'.exchange('good', 'bad')
d) 'Life is good'.change('good', 'bad')

Answer:

a) 'Life is good'.replace('good', 'bad')

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?

a) trim()
b) cut()
c) strip()
d) clean()

Answer:

c) strip()

Explanation:

The strip() method removes leading and trailing whitespaces from a string.

9. Which of the following strings is a palindrome?

a) 'Python'
b) 'madam'
c) 'world'
d) 'string'

Answer:

b) 'madam'

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()?

a) True
b) False
c) 1
d) 0

Answer:

a) True

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?

a) 'Hello World'.list()
b) list('Hello World')
c) 'Hello World'.split()
d) 'Hello World'.toArray()

Answer:

c) 'Hello World'.split()

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?

a) 'Python'[::-1]
b) 'Python'.reverse()
c) reverse('Python')
d) 'Python'[-1:]

Answer:

a) 'Python'[::-1]

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'?

a) 'Hello'.position('o')
b) 'Hello'.place('o')
c) 'Hello'.index('o')
d) 'Hello'.loc('o')

Answer:

c) 'Hello'.index('o')

Explanation:

The index() method returns the position at the first occurrence of the specified value.

14. What does ' '.isspace() return?

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

Answer:

a) True

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?

a) s.isalnum() and len(s) > 0
b) s.isalnum() or len(s) > 0
c) s.isalpha() and len(s) > 0
d) s.isnumeric() and len(s) > 0

Answer:

a) s.isalnum() and len(s) > 0

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?

a) Converts all characters in string to uppercase
b) Converts the first character in string to uppercase
c) Converts all characters in string to lowercase
d) Converts the first character in string to lowercase

Answer:

a) Converts all characters in string to uppercase

Explanation:

The str.upper() method converts all characters in the string to uppercase.

17. Which of these methods removes only trailing whitespace?

a) str.strip()
b) str.lstrip()
c) str.rstrip()
d) str.trim()

Answer:

c) str.rstrip()

Explanation:

The str.rstrip() method removes trailing whitespaces from a string.

18. What will '123'.isdigit() return?

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

Answer:

a) True

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?

a) str.find()
b) str.index()
c) str.search()
d) Both a) and b)

Answer:

d) Both a) and b)

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?

a) str.sub()
b) str.replace()
c) str.swap()
d) str.modify()

Answer:

b) str.replace()

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()?

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

Answer:

c) Python

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?

a) separate(',')
b) divide(',')
c) partition(',')
d) split(',')

Answer:

d) split(',')

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?

a) count('a')
b) find('a')
c) index('a')
d) locate('a')

Answer:

a) count('a')

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!


Comments