Wednesday, July 3, 2024

30+ MCQs on Python String Manipulation

Welcome to the Python String Manipulation MCQ Quiz! String manipulation is a vital side of programming, permitting builders to change, concatenate, search, and extract info from strings effectively. Python offers a wealthy set of built-in capabilities and strategies for string manipulation, making it a strong language for dealing with textual content knowledge. This quiz goals to check your understanding of varied ideas associated to Python string manipulation, together with string strategies, formatting, slicing, concatenation, and common expressions. Every query is multiple-choice, with just one right reply. Take your time to fastidiously learn every query and select the best choice. Let’s discover the world of Python string manipulation collectively!

Python String Manipulation

30+ MCQs on Python String Manipulation

Q1. What does the next Python code snippet do?

string = "Whats up, World!"
print(string.higher())

a) Prints “Whats up, World!” in lowercase

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

c) Reverses the string “Whats up, World!”

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

Reply: b

Clarification: The higher() methodology converts all characters within the string to uppercase.

Q2. How are you going to concatenate two strings in Python?

a) Utilizing the concat() methodology

b) Utilizing the be a part of() methodology

c) Utilizing the & operator

d) Utilizing the + operator

Reply: d

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

Q3. What’s the output of the next Python code snippet?

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

a) “Whats up”

b) “lo, “

c) “lo, W”

d) “lo, World”

Reply: b

Clarification: Slicing is used to extract a substring from a string. The indices 3:7 characterize the substring ranging from index 3 (inclusive) as much as index 7 (unique).

This fall. Which methodology is used to separate a string into a listing of substrings primarily based on a delimiter in Python?

a) cut up()

b) substring()

c) separate()

d) divide()

Reply: a

Clarification: The cut up() methodology is used to separate a string into a listing of substrings primarily based on a delimiter.

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

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

b) Removes all characters besides alphabets from the string

c) Converts the string to uppercase

d) Converts the string to lowercase

Reply: a

Clarification: The strip() methodology removes main and trailing whitespace characters from the string.

Q6. How are you going to test if a string incorporates a selected substring in Python?

a) Utilizing the incorporates() methodology

b) Utilizing the in key phrase

c) Utilizing the search() operate

d) Utilizing the hasSubstring() methodology

Reply: b

Clarification: The in key phrase is used to test if a substring exists inside a string in Python.

Q7. What does the change() methodology do in Python string manipulation?

a) Deletes all occurrences of a substring from the string

b) Replaces the primary prevalence of a substring with one other substring

c) Replaces all occurrences of a substring with one other substring

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

Reply: c

Clarification: The change() methodology replaces all occurrences of a substring with one other substring within the string.

Q8. What’s the output of the next Python code snippet?

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

a) ” Whats up, World!”

b) “Whats up, World! “

c) ” Whats up, World! “

d) “Whats up, World! “

Reply: a

Clarification: The rjust() methodology right-aligns the string in a area of width 20 by padding it with areas on the left.

Q9. Which methodology is used to search out the index of the primary prevalence of a substring in a string in Python?

a) discover()

b) search()

c) index()

d) find()

Reply: a

Clarification: The discover() methodology returns the index of the primary prevalence of a substring within the string, or -1 if the substring is just not discovered.

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

a) Checks if all characters within the string are digits

b) Converts the string to lowercase

c) Checks if all characters within the string are alphabets

d) Checks if the string is empty

Reply: a

Clarification: The isdigit() methodology returns True if all characters within the string are digits, in any other case False.

Q11. What’s the output of the next Python code snippet?

string = "Whats up, World!"
print(string.cut up(","))

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

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

c) [“Hello”]

d) [“World!”]

Reply: a

Clarification: The cut up() methodology splits the string into a listing of substrings primarily based on the desired delimiter (, on this case).

Q12. How are you going to test if a string begins with a selected substring in Python?

a) Utilizing the startswith() methodology

b) Utilizing the beginwith() methodology

c) Utilizing the begin() methodology

d) Utilizing the isstart() methodology

Reply: a

Clarification: The startswith() methodology is used to test if a string begins with a selected substring in Python.

Q13. What does the be a part of() methodology do in Python string manipulation?

a) Joins the weather of a listing right into a single string

b) Splits the string into a listing of substrings

c) Reverses the string

d) Converts the string to uppercase

Reply: a

Clarification: The be a part of() methodology joins the weather of a listing right into a single string utilizing the desired delimiter.

Q14. How are you going to take away main whitespace characters from a string in Python?

a) Utilizing the trim() methodology

b) Utilizing the remove_leading_whitespace() methodology

c) Utilizing the lstrip() methodology

d) Utilizing the strip() methodology

Reply: c

Clarification: The lstrip() methodology removes main whitespace characters from the string.

Q15. Which methodology is used to transform the primary character of a string to uppercase in Python?

a) higher()

b) capitalize()

c) title()

d) first_upper()

Reply: b

Clarification: The capitalize() methodology converts the primary character of a string to uppercase.

Q16. How are you going to test if a string ends with a selected substring in Python?

a) Utilizing the endwith() methodology

b) Utilizing the isend() methodology

c) Utilizing the endswith() methodology

d) Utilizing the isendwith() methodology

Reply: c

Clarification: The endswith() methodology is used to test if a string ends with a selected substring in Python.

Q17. What’s the output of the next Python code snippet?

string = "Whats up, World!"
print(string.discover("o"))

a) 4

b) 6

c) 7

d) -1

Reply: a

Clarification: The discover() methodology returns the index of the primary prevalence of a substring within the string, or -1 if the substring is just not discovered.

Q18. Which methodology is used to transform all characters in a string to lowercase in Python?

a) decrease()

b) uppercase()

c) to_lower()

d) toLower()

Reply: a

Clarification: The decrease() methodology is used to transform all characters in a string to lowercase.

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

a) Checks if all characters within the string are alphabets

b) Checks if all characters within the string are digits

c) Checks if the string is empty

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

Reply: a

Clarification: The isalpha() methodology returns True if all characters within the string are alphabets, in any other case False.

Q20. What’s the output of the next Python code snippet?

string = "Whats up, World!"
print(string.change("World", "Python"))

a) “Whats up, Python!”

b) “Whats up, World!”

c) “Python, World!”

d) “Python, Python!”

Reply: a

Clarification: The change() methodology replaces all occurrences of a substring with one other substring within the string.

Q21. How are you going to test if all characters in a string are digits in Python?

a) Utilizing the isdigit() methodology

b) Utilizing the isnumeric() methodology

c) Utilizing the isnumber() methodology

d) Utilizing the all_digits() methodology

Reply: a

Clarification: The isdigit() methodology returns True if all characters within the string are digits, in any other case False.

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

a) Converts the string to uppercase

b) Converts the string to lowercase

c) Capitalizes the primary character of every phrase within the string

d) Reverses the string

Reply: c

Clarification: The title() methodology capitalizes the primary character of every phrase within the string.

Q23. What’s the output of the next Python code snippet?

string = "  Whats up, World!  "
print(string.strip())

a) “Whats up, World!”

b) “Whats up, World!”

c) ” Whats up, World! “

d) “Whats up, World! “

Reply: a

Clarification: The strip() methodology removes main and trailing whitespace characters from the string.

Q24. Which methodology is used to test if a string incorporates solely whitespace characters in Python?

a) isspace()

b) iswhitespace()

c) isblank()

d) isemptyspace()

Reply: a

Clarification: The isspace() methodology returns True if all characters within the string are whitespace characters, in any other case False.

Q25. What’s the output of the next Python code snippet?

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

a) “Whats up, World!”

b) “dlroW ,olleH”

c) “World! Whats up,”

d) “olleH ,dlroW”

Reply: b

Clarification: Slicing with a unfavourable step (-1) reverses the string.

a) string[-2]

b) string[2:]

c) string[-2:]

d) string[:-2]

Reply: c

Clarification: Slicing with a unfavourable step (-1) reverses the string.

Q27. What’s the output of the next Python code snippet?

string = "Whats up, World!"
print(len(string))

a) 12

b) 13

c) 11

d) 10

Reply: b

Clarification: The len() operate returns the variety of characters within the string.

Q28. Which methodology is used to transform a string to a listing of characters in Python?

a) cut up()

b) listing()

c) chars()

d) tolist()

Reply: b

Clarification: The listing() operate converts the string to a listing of characters.

Q29. What’s the output of the next Python code snippet?

string = "Whats up, World!"
print(string.capitalize())

a) “hiya, world!”

b) “Whats up, world!”

c) “hiya, World!”

d) “Whats up, World!”

Reply: d

Clarification: The capitalize() methodology capitalizes the primary character of the string.

Q30. How are you going to test if a string is empty in Python?

a) Utilizing the isempty() methodology

b) Utilizing the empty() methodology

c) Utilizing the is_empty() methodology

d) Utilizing the len() operate

Reply: d

Clarification: Checking the size of the string utilizing the len() operate is a standard option to test if a string is empty.

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

a) Swaps the case of all characters within the string

b) Converts the string to uppercase

c) Converts the string to lowercase

d) Swaps the case of the primary character within the string

Reply: a

Clarification: The swapcase() methodology swaps the case of all characters within the string.

Q32. What would be the output of the next Python code snippet?

string = "Whats up, World!"
print(string.partition(","))

a) (‘Whats up’, ‘,’, ‘World!’)

b) (‘Whats up, ‘, ‘World’, ‘!’)

c) (‘Whats up’, ‘World’, ”)

d) (‘Whats up’, ‘, World’, ”)

Reply: a

Clarification: The partition() methodology splits the string into three elements primarily based on the primary prevalence of the desired separator.

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

a) Pads the string with main zeroes to fill the desired width

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

c) Removes all zeroes from the string

d) Converts the string to lowercase

Reply: a

Clarification: The zfill() methodology pads the string with main zeroes to fill the desired width.

Congratulations on finishing the Python String Manipulation MCQ Quiz! String manipulation is a elementary ability for any Python programmer, because it allows you to work with textual content knowledge successfully. By mastering string manipulation strategies in Python, you may carry out varied duties equivalent to parsing enter, formatting output, extracting info, and performing textual content processing operations. Hold training and experimenting with Python’s string manipulation functionalities to change into proficient in dealing with strings inside your applications. If in case you have any questions or need to delve deeper into any subject, don’t hesitate to proceed your studying journey. Blissful coding!

It’s also possible to enroll in out free Python Course As we speak!

Learn our extra articles associated to MCQs in Python:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles