Wednesday, 22 November 2023

Python String MCQs Set 1

MCQs based on Python String


1. Which of the following methods is used to convert a string to uppercase?

A.   upper()

B.    capitalize()

C.    title()

D.   all()

 

2. Which of the following methods is used to remove whitespace from the beginning and end of a string?

A.   lstrip()

B.    rstrip()

C.    strip()

D.   replace()

 

3. Which of the following methods is used to count the number of occurrences of a substring in a string?

A.   count()

B.    find()

C.    index()

D.   rfind()

 

4. Which of the following methods is used to split a string into a list of words?

A.   split()

B.    splitlines()

C.    partition()

D.   rsplit()

 

5. Which of the following methods is used to format a string with placeholder values?

A.   format()

B.    f-strings

C.    % operator

D.   str.format()

 

6. Which of the following is the correct syntax for replacing all occurrences of a substring within a string with another substring?

A.   string.replace(old, new)

B.    replace(string, old, new)

C.    string.replaceAll(old, new)

D.   string.replaceSubstring(old, new)


7. Which of the following is the correct syntax for splitting a string into a list of words based on spaces?

A.   string.split()

B.    split(string)

C.    string.splitWords()

D.   string.wordSplit()

 


Answers:

1. (A) upper()

2. (C) strip()

3. (A) count()

4. (A) split()

5. (D) str.format()

6. (A) string.replace(old, new)

7. (A) string.split()


Back to Python MCQs >


 

No comments:

Post a Comment