30+ MCQs on Python String Manipulation

Ayushi Trivedi Last Updated : 25 Feb, 2024
7 min read

Welcome to the Python String Manipulation MCQ Quiz! String manipulation is a crucial aspect of programming, allowing developers to modify, concatenate, search, and extract information from strings efficiently. Python provides a rich set of built-in functions and methods for string manipulation, making it a powerful language for handling text data. This quiz aims to test your understanding of various concepts related to Python string manipulation, including string methods, formatting, slicing, concatenation, and regular expressions. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python string manipulation together!

Python String Manipulation

30+ MCQs on Python String Manipulation

Q1. What does the following Python code snippet do?

string = "Hello, World!"
print(string.upper())

a) Prints “Hello, World!” in lowercase

b) Converts “Hello, World!” to uppercase and prints it

c) Reverses the string “Hello, World!”

d) Removes all whitespace characters from the string “Hello, World!”

Answer: b

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

Q2. How can you concatenate two strings in Python?

a) Using the concat() method

b) Using the join() method

c) Using the & operator

d) Using the + operator

Answer: d

Explanation: The + operator is used for string concatenation in Python.

Q3. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string[3:7])

a) “Hello”

b) “lo, “

c) “lo, W”

d) “lo, World”

Answer: b

Explanation: Slicing is used to extract a substring from a string. The indices 3:7 represent the substring starting from index 3 (inclusive) up to index 7 (exclusive).

Q4. Which method is used to split a string into a list of substrings based on a delimiter in Python?

a) split()

b) substring()

c) separate()

d) divide()

Answer: a

Explanation: The split() method is used to split a string into a list of substrings based on a delimiter.

Q5. What does the strip() method do in Python string manipulation?

a) Removes all whitespace characters from both ends of the string

b) Removes all characters except alphabets from the string

c) Converts the string to uppercase

d) Converts the string to lowercase

Answer: a

Explanation: The strip() method removes leading and trailing whitespace characters from the string.

Q6. How can you check if a string contains a specific substring in Python?

a) Using the contains() method

b) Using the in keyword

c) Using the search() function

d) Using the hasSubstring() method

Answer: b

Explanation: The in keyword is used to check if a substring exists within a string in Python.

Q7. What does the replace() method do in Python string manipulation?

a) Deletes all occurrences of a substring from the string

b) Replaces the first occurrence of a substring with another substring

c) Replaces all occurrences of a substring with another substring

d) Inserts a substring into the string at a specified position

Answer: c

Explanation: The replace() method replaces all occurrences of a substring with another substring in the string.

Q8. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string.rjust(20))

a) ” Hello, World!”

b) “Hello, World! “

c) ” Hello, World! “

d) “Hello, World! “

Answer: a

Explanation: The rjust() method right-aligns the string in a field of width 20 by padding it with spaces on the left.

Q9. Which method is used to find the index of the first occurrence of a substring in a string in Python?

a) find()

b) search()

c) index()

d) locate()

Answer: a

Explanation: The find() method returns the index of the first occurrence of a substring in the string, or -1 if the substring is not found.

Q10. What does the isdigit() method do in Python string manipulation?

a) Checks if all characters in the string are digits

b) Converts the string to lowercase

c) Checks if all characters in the string are alphabets

d) Checks if the string is empty

Answer: a

Explanation: The isdigit() method returns True if all characters in the string are digits, otherwise False.

Q11. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string.split(","))

a) [“Hello”, “World!”]

b) [“Hello,”, “World!”]

c) [“Hello”]

d) [“World!”]

Answer: a

Explanation: The split() method splits the string into a list of substrings based on the specified delimiter (, in this case).

Q12. How can you check if a string starts with a specific substring in Python?

a) Using the startswith() method

b) Using the beginwith() method

c) Using the start() method

d) Using the isstart() method

Answer: a

Explanation: The startswith() method is used to check if a string starts with a specific substring in Python.

Q13. What does the join() method do in Python string manipulation?

a) Joins the elements of a list into a single string

b) Splits the string into a list of substrings

c) Reverses the string

d) Converts the string to uppercase

Answer: a

Explanation: The join() method joins the elements of a list into a single string using the specified delimiter.

Q14. How can you remove leading whitespace characters from a string in Python?

a) Using the trim() method

b) Using the remove_leading_whitespace() method

c) Using the lstrip() method

d) Using the strip() method

Answer: c

Explanation: The lstrip() method removes leading whitespace characters from the string.

Q15. Which method is used to convert the first character of a string to uppercase in Python?

a) upper()

b) capitalize()

c) title()

d) first_upper()

Answer: b

Explanation: The capitalize() method converts the first character of a string to uppercase.

Q16. How can you check if a string ends with a specific substring in Python?

a) Using the endwith() method

b) Using the isend() method

c) Using the endswith() method

d) Using the isendwith() method

Answer: c

Explanation: The endswith() method is used to check if a string ends with a specific substring in Python.

Q17. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string.find("o"))

a) 4

b) 6

c) 7

d) -1

Answer: a

Explanation: The find() method returns the index of the first occurrence of a substring in the string, or -1 if the substring is not found.

Q18. Which method is used to convert all characters in a string to lowercase in Python?

a) lower()

b) uppercase()

c) to_lower()

d) toLower()

Answer: a

Explanation: The lower() method is used to convert all characters in a string to lowercase.

Q19. What does the isalpha() method do in Python string manipulation?

a) Checks if all characters in the string are alphabets

b) Checks if all characters in the string are digits

c) Checks if the string is empty

d) Checks if the string contains any non-alphanumeric characters

Answer: a

Explanation: The isalpha() method returns True if all characters in the string are alphabets, otherwise False.

Q20. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string.replace("World", "Python"))

a) “Hello, Python!”

b) “Hello, World!”

c) “Python, World!”

d) “Python, Python!”

Answer: a

Explanation: The replace() method replaces all occurrences of a substring with another substring in the string.

Q21. How can you check if all characters in a string are digits in Python?

a) Using the isdigit() method

b) Using the isnumeric() method

c) Using the isnumber() method

d) Using the all_digits() method

Answer: a

Explanation: The isdigit() method returns True if all characters in the string are digits, otherwise False.

Q22. What does the title() method do in Python string manipulation?

a) Converts the string to uppercase

b) Converts the string to lowercase

c) Capitalizes the first character of each word in the string

d) Reverses the string

Answer: c

Explanation: The title() method capitalizes the first character of each word in the string.

Q23. What is the output of the following Python code snippet?

string = "  Hello, World!  "
print(string.strip())

a) “Hello, World!”

b) “Hello, World!”

c) ” Hello, World! “

d) “Hello, World! “

Answer: a

Explanation: The strip() method removes leading and trailing whitespace characters from the string.

Q24. Which method is used to check if a string contains only whitespace characters in Python?

a) isspace()

b) iswhitespace()

c) isblank()

d) isemptyspace()

Answer: a

Explanation: The isspace() method returns True if all characters in the string are whitespace characters, otherwise False.

Q25. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string[::-1])

a) “Hello, World!”

b) “dlroW ,olleH”

c) “World! Hello,”

d) “olleH ,dlroW”

Answer: b

Explanation: Slicing with a negative step (-1) reverses the string.

Q26. How can you extract the last two characters from a string in Python?

a) string[-2]

b) string[2:]

c) string[-2:]

d) string[:-2]

Answer: c

Explanation: Slicing with a negative step (-1) reverses the string.

Q27. What is the output of the following Python code snippet?

string = "Hello, World!"
print(len(string))

a) 12

b) 13

c) 11

d) 10

Answer: b

Explanation: The len() function returns the number of characters in the string.

Q28. Which method is used to convert a string to a list of characters in Python?

a) split()

b) list()

c) chars()

d) tolist()

Answer: b

Explanation: The list() function converts the string to a list of characters.

Q29. What is the output of the following Python code snippet?

string = "Hello, World!"
print(string.capitalize())

a) “hello, world!”

b) “Hello, world!”

c) “hello, World!”

d) “Hello, World!”

Answer: d

Explanation: The capitalize() method capitalizes the first character of the string.

Q30. How can you check if a string is empty in Python?

a) Using the isempty() method

b) Using the empty() method

c) Using the is_empty() method

d) Using the len() function

Answer: d

Explanation: Checking the length of the string using the len() function is a common way to check if a string is empty.

Q31. What does the swapcase() method do in Python string manipulation?

a) Swaps the case of all characters in the string

b) Converts the string to uppercase

c) Converts the string to lowercase

d) Swaps the case of the first character in the string

Answer: a

Explanation: The swapcase() method swaps the case of all characters in the string.

Q32. What will be the output of the following Python code snippet?

string = "Hello, World!"
print(string.partition(","))

a) (‘Hello’, ‘,’, ‘World!’)

b) (‘Hello, ‘, ‘World’, ‘!’)

c) (‘Hello’, ‘World’, ”)

d) (‘Hello’, ‘, World’, ”)

Answer: a

Explanation: The partition() method splits the string into three parts based on the first occurrence of the specified separator.

Q33. What does the zfill() method do in Python string manipulation?

a) Pads the string with leading zeroes to fill the specified width

b) Pads the string with trailing zeroes to fill the specified width

c) Removes all zeroes from the string

d) Converts the string to lowercase

Answer: a

Explanation: The zfill() method pads the string with leading zeroes to fill the specified width.

Congratulations on completing the Python String Manipulation MCQ Quiz! String manipulation is a fundamental skill for any Python programmer, as it enables you to work with text data effectively. By mastering string manipulation techniques in Python, you can perform various tasks such as parsing input, formatting output, extracting information, and performing text processing operations. Keep practicing and experimenting with Python’s string manipulation functionalities to become proficient in handling strings within your programs. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details