Python String MCQ
1). Which of the following methods is used to find the length of a string in Python?
a) length()
b) len()
c) size()
d) count()
Correct answer is: b) len()
Explanation: The len() function returns the length of a string in Python.
2). What does the “+” operator do when used with strings in Python?
a) Concatenates two strings
b) Reverses a string
c) Finds the first occurrence of a substring
d) Splits a string into a list of substrings
Correct answer is: a) Concatenates two strings
Explanation: The “+” operator concatenates two strings, joining them together.
3). Which of the following methods is used to convert a string to uppercase in Python?
a) to_lower()
b) lower()
c) upper()
d) convert_to_upper()
Correct answer is: c) upper()
Explanation: The upper() method converts a string to uppercase in Python.
4). In Python which method is used to check if a string starts with a specific substring?
a) startswith()
b) contains()
c) startswiths()
d) beginswith()
Correct answer is: a) startswith()
Explanation: The startswith() method checks if a string starts with a specific substring.
5). How can you access individual characters in a string in Python?
a) Using the [] operator with an index
b) Using the dot operator
c) Using the slice operator
d) Using the get() method
Correct answer is: a) Using the [] operator with an index
Explanation: Individual characters in a string can be accessed using the [] operator with an index.
6). Which method is used to remove leading and trailing whitespace from a string in Python?
a) trim()
b) strip()
c) remove()
d) clean()
Correct answer is: b) strip()
Explanation: The strip() method removes leading and trailing whitespace from a string in Python.
7). Which method is used to split a string into a list of substrings based on a delimiter?
a) split()
b) join()
c) separate()
d) divide()
Correct answer is: a) split()
Explanation: The split() method splits a string into a list of substrings based on a delimiter.
8). What does the “in” keyword do when used with strings in Python?
a) Checks if a string is empty
b) Checks if a string is uppercase
c) Checks if a substring is present in a string
d) Checks if a string is numeric
Correct answer is: c) Checks if a substring is present in a string
Explanation: The “in” keyword checks if a substring is present in a string in Python.
9). Which method is used to convert an integer to a string in Python?
a) int_to_string()
b) convert_to_string()
c) str()
d) to_string()
Correct answer is: c) str()
Explanation: The str() function converts an integer to a string in Python.
10). Which method is used to check if a string contains only numeric characters?
a) isdigit()
b) isnumeric()
c) isnumber()
d) isint()
Correct answer is: a) isdigit()
Explanation: The isdigit() method checks if a string contains only numeric characters.
11). Which method is used to count the occurrences of a substring in a string?
a) count()
b) find()
c) search()
d) match()
Correct answer is: a) count()
Explanation: The count() method returns the number of occurrences of a substring in a string.
12). Which method is used to check if a string ends with a specific substring?
a) endswith()
b) finishswith()
c) checkends()
d) hasending()
Correct answer is: a) endswith()
Explanation: The endswith() method checks if a string ends with a specific substring.
13). Which method is used to check if a string contains only alphabetic characters?
a) isalpha()
b) isalphabetic()
c) isletter()
d) isalphastring()
Correct answer is: a) isalpha()
Explanation: The isalpha() method checks if a string contains only alphabetic characters.
14). What is the output of the following code?
text = “Hello World”
print(text.capitalize())
a) Hello World
b) hello world
c) Hello world
d) Hello World
Correct answer is: d) Hello World
Explanation: The capitalize() method capitalizes the first character of a string.
15). In Python which method is used to check if a string contains only whitespace characters?
a) iswhitespace()
b) isblank()
c) isempty()
d) isspace()
Correct answer is: d) isspace()
Explanation: The isspace() method checks if a string contains only whitespace characters.
16). Which method is used to repeat a string a specified number of times?
a) repeat()
b) replicate()
c) duplicate()
d) multiply()
Correct answer is: d) multiply()
Explanation: In Python the “*” operator can be used to repeat a string a specified number of times.
17). Which method is used to check if a string contains only lowercase characters?
a) islower()
b) islowercase()
c) islowerchar()
d) islc()
Correct answer is: a) islower()
Explanation: The islower() method checks if all characters in a string are lowercase.
18). What is the output of the following code?
text = “Hello World”
print(text.join(“-“))
a) Hello World!
b) Hello-World!
c) H-e-l-l-o- -W-o-r-l-d
d) Error
Correct answer is: c) H-e-l-l-o-W-o-r-l-d
Explanation: The join() method joins each character of a string with a specified delimiter.
19). Which method is used to check if a string contains only titlecased characters?
a) istitlecase()
b) istitle()
c) iscapitalized()
d) istitlechar()
Correct answer is: b) istitle()
Explanation: The istitle() method checks if a string contains only titlecased characters.
20). What is the output of the following code?
text = “learning Python!”
print(text.title())
a) learning, python!
b) Learning Python!
c) learning Python!
d) LEARNING PYTHON!
Correct answer is: b) Learning Python!
Explanation: The title() method capitalizes the first character of each word in a string.
21). Which method is used to replace a specified number of occurrences of a substring in a string?
a) replace()
b) substitute()
c) change()
d) modify()
Correct answer is: a) replace()
Explanation: The replace() method replaces a specified number of occurrences of a substring with another substring.
22). What is the output of the following code?
text = “Hello World”
print(text.endswith(“d”))
a) True
b) False
c) None
d) Error
Correct answer is: a) True
Explanation: The endswith() method checks if a string ends with a specific substring.
23). Which method is used to remove a specific character from a string?
a) remove()
b) delete()
c) strip()
d) replace()
Correct answer is: d) replace()
Explanation: The replace() method can be used to remove a specific character from a string by replacing it with an empty string.
24). What is the output of the following code?
text = “Hello World”
print(text.lower())
a) Hello World
b) hello world
c) Hello world
d) hello World
Correct answer is: b) hello world
Explanation: The lower() method converts all characters in a string to lowercase.
25). Which method is used to check if a string is a valid identifier in Python?
a) isidentifier()
b) isvalididentifier()
c) isname()
d) isvarname()
Correct answer is: a) isidentifier()
Explanation: The isidentifier() method checks if a string is a valid Python identifier.