Home »
MCQs
Python String Manipulation MCQs (Multiple-Choice Questions)
Python String Manipulation involves working with text using strings, indexing, slicing, concatenation, formatting, searching, replacing, and various built-in string methods. Python provides a rich set of operations for processing and transforming string data.
Python String Manipulation MCQs
These Python String Manipulation MCQs cover important concepts such as string indexing, slicing, concatenation, formatting, searching, splitting, joining, case conversion, and string validation methods.
List of Python String Manipulation MCQs
The following Python String Manipulation multiple-choice questions are designed to test your understanding of commonly used string operations and methods.
1. Which data type is used to represent text in Python?
- str
- text
- string
- char
Answer: A) str
Explanation:
Python uses the str data type to represent sequences of Unicode characters.
2. Which of the following correctly creates a string in Python?
"Hello"
Hello
<Hello>
#Hello
Answer: A) "Hello"
Explanation:
Strings can be created using single quotes, double quotes, or triple quotes in Python.
3. What is the output of the following code?
text = "Python"
print(text[0])
- P
- y
- n
- Python
Answer: A) P
Explanation:
Python string indexing starts at 0, so index 0 refers to the first character of the string.
4. What is the output of the following code?
text = "Python"
print(text[-1])
- P
- n
- o
- Error
Answer: B) n
Explanation:
Negative indexing starts from the end of a string. The index -1 refers to the last character.
5. What is the output of the following code?
text = "Python"
print(text[1:4])
- Pyt
- yth
- ytho
- thon
Answer: B) yth
Explanation:
The slice [1:4] starts at index 1 and stops before index 4, producing yth.
6. Which operator is used to concatenate strings in Python?
- *
- +
- &
- %
Answer: B) +
Explanation:
The + operator concatenates two or more strings.
7. What is the output of the following code?
a = "Hello"
b = "Python"
print(a + " " + b)
- HelloPython
- Hello Python
- Hello+Python
- Error
Answer: B) Hello Python
Explanation:
The + operator joins the strings, while the string containing a space adds the space between them.
8. Which function returns the number of characters in a string?
- size()
- count()
- len()
- length()
Answer: C) len()
Explanation:
The built-in len() function returns the number of characters in a string.
9. What is the output of the following code?
text = "Python"
print(len(text))
- 5
- 6
- 7
- 8
Answer: B) 6
Explanation:
The string Python contains six characters.
10. Which method converts all characters in a string to uppercase?
- upper()
- uppercase()
- toUpper()
- capital()
Answer: A) upper()
Explanation:
The upper() method returns a copy of the string with alphabetic characters converted to uppercase.
11. Which method converts all characters in a string to lowercase?
- lower()
- lowercase()
- toLower()
- small()
Answer: A) lower()
Explanation:
The lower() method returns a copy of the string with alphabetic characters converted to lowercase.
12. What does the capitalize() method do?
- Converts the entire string to uppercase
- Capitalizes the first character and lowercases the remaining characters
- Capitalizes every word
- Removes spaces
Answer: B) Capitalizes the first character and lowercases the remaining characters
Explanation:
The capitalize() method returns a copy with the first character in titlecase and the remaining characters in lowercase.
13. Which method converts the first character of each word to uppercase?
- capitalize()
- title()
- upper()
- wordcase()
Answer: B) title()
Explanation:
The title() method returns a titlecased version of the string.
14. What does the swapcase() method do?
- Swaps the positions of characters
- Converts uppercase characters to lowercase and lowercase characters to uppercase
- Converts all characters to uppercase
- Reverses the string
Answer: B) Converts uppercase characters to lowercase and lowercase characters to uppercase
Explanation:
The swapcase() method reverses the case of alphabetic characters.
15. Which method removes leading and trailing whitespace from a string?
- trim()
- strip()
- remove()
- clean()
Answer: B) strip()
Explanation:
The strip() method returns a copy of the string with leading and trailing whitespace removed.
16. Which method removes whitespace from the beginning of a string?
- strip()
- rstrip()
- lstrip()
- leftstrip()
Answer: C) lstrip()
Explanation:
The lstrip() method removes leading whitespace or specified characters from the left side of a string.
17. Which method removes whitespace from the end of a string?
- lstrip()
- rstrip()
- endstrip()
- stripright()
Answer: B) rstrip()
Explanation:
The rstrip() method removes trailing whitespace or specified characters from the right side of a string.
18. Which method replaces occurrences of one substring with another?
- replace()
- change()
- substitute()
- modify()
Answer: A) replace()
Explanation:
The replace() method returns a new string in which occurrences of the specified substring are replaced.
19. What is the output of the following code?
text = "Python Python"
print(text.replace("Python", "Java"))
- Python Java
- Java Python
- Java Java
- Python Python
Answer: C) Java Java
Explanation:
Without a count argument, replace() replaces all occurrences of the specified old substring.
20. Which method finds the first occurrence of a substring and returns its index?
- find()
- search()
- locate()
- indexof()
Answer: A) find()
Explanation:
The find() method returns the lowest index where the specified substring is found, or -1 if it is not found.
21. What does the index() method return when the substring is not found?
- -1
- 0
- None
- It raises a ValueError
Answer: D) It raises a ValueError
Explanation:
Unlike find(), the index() method raises a ValueError if the specified substring is not found.
22. Which operator checks whether a substring exists within a string?
- contains
- in
- has
- exists
Answer: B) in
Explanation:
The in operator can be used to test whether one string is contained within another string.
23. What is the output of the following code?
text = "Python Programming"
print("Python" in text)
- True
- False
- None
- Error
Answer: A) True
Explanation:
The substring Python exists within the string, so the membership test returns True.
24. Which method checks whether a string starts with a specified substring?
- startswith()
- startwith()
- beginwith()
- prefix()
Answer: A) startswith()
Explanation:
The startswith() method returns True if the string starts with the specified prefix.
25. Which method checks whether a string ends with a specified substring?
- ends()
- endwith()
- endswith()
- suffix()
Answer: C) endswith()
Explanation:
The endswith() method returns True if the string ends with the specified suffix.
26. Which method splits a string into a list of substrings?
- split()
- divide()
- separate()
- break()
Answer: A) split()
Explanation:
The split() method breaks a string into a list of substrings using a specified separator or whitespace by default.
27. What is the output of the following code?
text = "Python,Java,C++"
print(text.split(","))
- ['Python', 'Java', 'C++']
- ['Python,Java,C++']
- ['Python', 'Java,C++']
- ('Python', 'Java', 'C++')
Answer: A) ['Python', 'Java', 'C++']
Explanation:
The comma is used as the separator, so split(",") produces a list containing three strings.
28. Which method joins elements of an iterable into a string using a separator?
- join()
- combine()
- merge()
- concat()
Answer: A) join()
Explanation:
The join() method concatenates strings from an iterable, placing the calling string between each element.
29. What is the output of the following code?
words = ["Python", "is", "easy"]
print(" ".join(words))
- Pythoniseasy
- Python is easy
- Python-is-easy
- ['Python is easy']
Answer: B) Python is easy
Explanation:
The space string is used as the separator, so the elements are joined with spaces between them.
30. Which method checks whether all characters in a string are digits?
- isnumber()
- isdigit()
- isnumericstring()
- isint()
Answer: B) isdigit()
Explanation:
The isdigit() method returns True when all characters in the string are digits and the string is non-empty.
31. Which method checks whether all characters in a string are alphabetic?
- isalpha()
- isalphabet()
- letters()
- isletters()
Answer: A) isalpha()
Explanation:
The isalpha() method returns True when all characters are alphabetic and the string is non-empty.
32. Which method checks whether a string contains only alphanumeric characters?
- isalnum()
- isalpha()
- isnumeric()
- isalphanumeric()
Answer: A) isalnum()
Explanation:
The isalnum() method returns True if all characters are alphanumeric and the string is non-empty.
33. Which method checks whether all characters in a string are whitespace characters?
- isspace()
- isblank()
- whitespace()
- onlyspace()
Answer: A) isspace()
Explanation:
The isspace() method returns True when all characters in the string are whitespace characters and the string is not empty.
34. What does the count() method return?
- The number of occurrences of a substring
- The length of the string
- The number of words only
- The number of unique characters
Answer: A) The number of occurrences of a substring
Explanation:
The count() method returns the number of non-overlapping occurrences of a specified substring.
35. What is the output of the following code?
text = "banana"
print(text.count("a"))
- 2
- 3
- 4
- 5
Answer: B) 3
Explanation:
The character a appears three times in the string banana.
36. Which method returns a string centered in a field of a specified width?
- center()
- align()
- middle()
- center_text()
Answer: A) center()
Explanation:
The center() method returns a centered string padded to the specified width.
37. Which method right-aligns a string within a specified width?
- right()
- rjust()
- alignright()
- justify_right()
Answer: B) rjust()
Explanation:
The rjust() method returns the string right-justified in a field of the specified width.
38. Which method pads a string with zeros on the left?
- padzero()
- zfill()
- zerofill()
- fillzero()
Answer: B) zfill()
Explanation:
The zfill() method pads a numeric string with zeros on the left to reach the specified width.
39. What is the output of the following code?
text = "42"
print(text.zfill(5))
- 42000
- 00042
- 00420
- 42 000
Answer: B) 00042
Explanation:
The string has a width of 2, so zfill(5) adds three zeros to the left.
40. Which feature is used to insert expressions directly into string literals?
- f-strings
- r-strings
- b-strings
- u-strings
Answer: A) f-strings
Explanation:
Formatted string literals, commonly called f-strings, allow expressions to be embedded inside string literals using braces.
41. What is the output of the following code?
name = "John"
print(f"Hello, {name}!")
- Hello, name!
- Hello, John!
- fHello, John!
- Error
Answer: B) Hello, John!
Explanation:
The f-string evaluates the expression inside the braces and inserts its value into the resulting string.
42. Which method provides another way to format strings using replacement fields?
- format()
- format_string()
- printf()
- insert()
Answer: A) format()
Explanation:
The str.format() method uses replacement fields enclosed in braces to insert values into a string.
43. What is the output of the following code?
name = "Alice"
print("Hello, {}!".format(name))
- Hello, {}!
- Hello, Alice!
- Hello, name!
- Error
Answer: B) Hello, Alice!
Explanation:
The empty replacement field {} is replaced by the first positional argument supplied to format().
44. Which prefix is used to create a raw string literal in Python?
- r
- raw
- x
- RSTR
Answer: A) r
Explanation:
An r prefix creates a raw string literal, which is commonly useful when working with backslashes and regular-expression patterns.
45. Which Python module provides support for regular expressions?
- regexlib
- re
- regexp
- pattern
Answer: B) re
Explanation:
The built-in re module provides support for regular expression operations in Python.
46. Which re function searches for a match anywhere in a string?
- re.search()
- re.findallonly()
- re.locate()
- re.scan()
Answer: A) re.search()
Explanation:
The re.search() function scans through the string and returns a match object for the first location where the pattern matches.
47. Which re function returns all non-overlapping matches of a pattern as a list?
- re.findall()
- re.searchall()
- re.matchall()
- re.all()
Answer: A) re.findall()
Explanation:
The re.findall() function returns all non-overlapping matches of a regular expression pattern.
48. Are Python strings mutable?
- Yes, always
- No, strings are immutable
- Only strings longer than 10 characters are mutable
- Only strings created with single quotes are mutable
Answer: B) No, strings are immutable
Explanation:
Python strings are immutable. String operations such as replace() return a new string rather than modifying the existing string in place.
49. What is the output of the following code?
text = "Python"
text = text.upper()
print(text)
- Python
- python
- PYTHON
- Error
Answer: C) PYTHON
Explanation:
The upper() method returns a new uppercase string, which is then assigned back to text.
50. What is the output of the following code?
text = "Python"
print(text[::-1])
- Python
- nohtyP
- nothyP
- Error
Answer: B) nohtyP
Explanation:
The slice [::-1] uses a step of -1, which traverses the string from the last character to the first and therefore reverses it.
Advertisement
Advertisement