STRING FUNCTIONS
_A. AJAY
📝 Python String Functions – MCQs (Competitive & Placement Style)
Q1.
Function that removes both leading and trailing whitespace from a string:
a) trim()
b) strip()
c) rstrip()
d) lstrip()
Q2.
Output of the following code:
Copy code
Python
S = “Python”
Print(s[1:4])
a) yth
b) Pyt
c) ytho
d) ythn
Q3.
Method used to check if all characters in a string are digits:
a) isdigit()
b) isnumeric()
c) isdecimal()
d) All of the above
Q4.
Result of the code snippet:
Copy code
Python
S = “HelloWorld”
Print(s.lower().count(“l”))
a) 2
b) 3
c) 4
d) 5
Q5.
Statement about Python strings that is correct:
a) Strings are immutable.
b) Strings are mutable.
c) Strings can be modified using indexing.
d) Strings are numeric data types.
Q6.
Evaluation of the expression:
Copy code
Python
S = “123abc”
Print(s.isalnum())
a) True
b) False
c) 123
d) Error
Q7.
Function returning index of first occurrence of a substring or -1 if not found:
a) index()
b) find()
c) search()
d) locate()
Q8.
Result of the string operation:
Copy code
Python
S = “banana”
Print(s.replace(“a”, “o”, 2))
a) bonono
b) banana
c) bonana
d) banono
Q9.
Property of the join() method:
a) Joins multiple strings into one string.
b) Joins characters of a string with a separator.
c) Used to join two lists.
d) Both (a) and (b).
Q10.
Output after execution of:
Copy code
Python
S = “Python”
Print(s.center(10, “*”))
a) **Python**
b) Python****
c) ****Python
d) **Python***
ANSWER
Q1. Function that removes both leading and trailing whitespace from a string
✔ b) strip()
Q2. Output of the code
Copy code
Python
s = "Python"
print(s[1:4])
✔ a) yth
Q3. Method used to check if all characters in a string are digits
✔ d) All of the above
Q4. Result of the code
Copy code
Python
s = "HelloWorld"
print(s.lower().count("l"))
✔ c) 4
Q5. Correct statement about Python strings
✔ a) Strings are immutable.
Q6. Evaluation of
Copy code
Python
s = "123abc"
print(s.isalnum())
✔ a) True
Q7. Function returning index of first occurrence of a substring or -1 if not
found
✔ b) find()
Q8. Result of the code
Copy code
Python
s = "banana"
print(s.replace("a", "o", 2))
✔ c) bonana
Q9. Property of the join() method
✔ d) Both (a) and (b).
Q10. Output of the code
Copy code
Python
s = "Python"
print(s.center(10, "*"))
✔ a) **Python**