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!
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.
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.
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).
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.
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.
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.
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.
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.
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.
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
.
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).
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.
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.
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.
a) upper()
b) capitalize()
c) title()
d) first_upper()
Answer: b
Explanation: The capitalize()
method converts the first character of a string to uppercase.
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.
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.
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.
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
.
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.
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
.
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.
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.
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
.
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.
a) string[-2]
b) string[2:]
c) string[-2:]
d) string[:-2]
Answer: c
Explanation: Slicing with a negative step (-1) reverses the string.
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.
a) split()
b) list()
c) chars()
d) tolist()
Answer: b
Explanation: The list()
function converts the string to a list of characters.
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.
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.
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.
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.
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: