Font size
WorksheetsPython String Operations and Methods
Total questions: 100
Worksheet time: 50mins
What is the output of the following Python code?
s = "cbse@2024"
count = 0
for char in s:
----if char.isdigit():
--------count += 1
print(count)
3
4
5
9
What is the output of the following Python code?
s = "Python"
result = ""
for i in range(len(s)):
------if i % 2 == 0:
---------result += s[i].upper()
-- - - else:
----------result += s[i].lower()
print(result)
"pYtHoN"
"PyThOn"
"PYTHON"
"pYtHon"
What is the output of the following Python code?
s = "hello PYTHON"
new_s = ""
for char in s:
----if char.islower():
------new_s += char.upper()
----elif char.isupper():
------new_s += char.lower()
----else:
-------new_s += ' '
print(new_s)
"HELLO python"
"HELLO*PYTHON"
"hELLO*python"
"HELLO*python"
What is the output of len(" Python ".strip())?
10
8
6
4
Which method converts the first character of a string to uppercase and the rest to lowercase?
title()
upper()
capitalize()
casefold()
If s = "InDiA", what will s.title() return?
"INDIA"
"india"
"InDia"
"India"
What is the key difference between str.find() and str.index()?
find() searches from the end, while index() searches from the beginning.
index() returns -1 if the substring is not found, while find() raises a ValueError.
find() returns -1 if the substring is not found, while index() raises a ValueError.
There is no difference; they are aliases for the same method.
Given s = "PYTHON", what is the output of s.lower().count('o')?
0
1
2
Error
If s = "mississippi", what is the output of s.find('ss', 4)?
2
5
4
-1
True or False: The len() function includes all characters, including spaces, when calculating the length of a string.
True
False
True or False: The upper() and lower() methods modify the original string in place.
True
False
True or False: If the substring is not found, both find() and index() will return -1.
True
False
True or False: The capitalize() method will convert all words in a string to have their first letter in uppercase.
True
False
Which method is used to check if a string consists only of alphabetic characters and digits (0-9)?
isalpha()
isdigit()
isalnum()
isnumeric()
What will s.startswith('A', 1, 4) return if s = "ABACUS"?
True
False
Error
None
For a string s to return True for s.isspace(), what must it contain?
Only digits and spaces.
Only whitespace characters (spaces, tabs, newlines).
At least one space character.
Only empty characters.
Given s = "2024", which one of the following will return True?
s.islower()
s.isalpha()
s.isalnum()
s.isupper()
Which method is the opposite of startswith()?
ends()
endswith()
stopwith()
stopexactly()
What is the output of the following Python code? Python s = "python is fun" print(s.count('p') + s.count('n'))
2
1
3
4
What is the output of the following Python code?
s = " \n "
print(s.isspace())
True
False
None
Error
What is the output of the following Python code?
s = "COMP"
print(s.isupper() and s.isalpha())
True
False
None
Error
What is the output of the following Python code?
s = "a1b2"
print(s.isalpha() or s.isdigit())
True
False
Error
None
What is the output of the following Python code?
s = "HelloWorld"
print(s.startswith("He") and s.endswith("ld"))
True
False
"He ld"
"HeWorld"
True or False: If a string contains a space, isalnum() will return False.
True
False
True or False: The string "12.34" will return True for the isdigit() method.
True
False
True or False: The expression islower() will return True for an empty string s = "".
True
False
True or False: The string "class-12" will return True for isalnum().
True
False
What is the output of the following Python code? s = "---Hello---" print(s.strip('-'))
"Hello"
"---Hello"
"Hello---"
"Hel"
What is the output of the following Python code?
s = "$$Data$$"
print(s.lstrip('$'))
"Data"
"Data$$"
"$$Data"
"Data$"
What is the output of the following Python code?
s = "one two one three one"
print(s.replace("one", "four", 2))
"four two one three one"
"four two four three one"
"four two four three four"
"four two one three four"
What is the output of the following Python code?
l = ["C", "B", "S", "E"]
print("-".join(l))
['C', 'B', 'S', 'E']
"CBSE"
"C-B-S-E"
Error
What is the output of the following Python code?
s = "A:B:C"
print(s.split(':'))
('A', 'B', 'C')
['A', 'B', 'C']
"A B C"
Error
What is the output of the following Python code?
s = "name,age,city"
parts = s.split(',', 1)
print(parts[1])
"age,city"
"name"
"city"
Error
What is the output of the following Python code?
s = "hello world"
print(s.partition(' '))
['hello', 'world']
('hello', 'world')
('hello', ' ', 'world')
['hello', ' ', 'world']
What is the output of the following Python code?
data = ("file", "txt")
print(".".join(data))
('file', '.', 'txt')
"file.txt"
["file", "txt"]
Error
If a string contains only spaces and tabs, what does strip() return?
A single space character.
The original string.
An empty string "".
None.
Which of the following is true about str.partition(separator)?
It returns a list of two elements.
It returns a tuple of two elements.
It returns a tuple of three elements.
It returns a list of three elements.
The maximum number of splits that str.split(delimiter, maxsplit) will perform is controlled by:
delimiter
maxsplit
The length of the string
The number of times the delimiter appears.
True or False: If the delimiter for split() is not found in the string, the method returns a list containing the original string as its only element.
True
False
True or False: The replace() method replaces all occurrences of the old substring by default.
True
False
True or False: lstrip() and rstrip() can be used to remove specific characters, not just whitespace.
True
False
True or False: The join() method can be used with a list containing a mix of string and integer elements.
True
False
What is the output of the following Python code?
s = "py thon"
print(s.replace(' ', '').upper())
"PY THON"
"PYTHON"
"PYTH ON"
"PY,THON"
What is the output of the following Python code?
s = "hello world"
print(s.find('o', 5, 10))
4
7
-1
6
What is the output of the following Python code?
s = " *** DATA *** "
print(s.strip().strip('*').strip())
"DATA"
"** DATA **"
"*** DATA ***"
Error
What is the output of the following Python code?
s = "A@B@C"
a, b, c = s.partition('@')
print(a, c)
"A B@C"
"A B C"
"A @ B@C"
"B@C A"
What is the output of the following Python code?
s = "Python Program"
print(s[7:].lower().index('p'))
0
1
7
Error
What is the output of the following Python code?
s = "123"
print(s.isdigit() and s.isalnum())
True
False
None
Error
What is the output of the following Python code?
s = "hello"
print(s.upper()[1:-1])
"ELLO"
"ELL"
"HELL"
"HELLO"
What is the output of the following Python code?
s = "COMPONENT"
print(s.replace('O', 'o', 1).lower())
"cOmpONENT"
"component"
"compONENT"
"coMPONENT"
What is the output of the following Python code?
s = "Welcome to India"
print(s.split('o'))
['Welc', 'me t', ' India']
['Welco', 'me', 'to', 'India']
['Welco', 'me to', ' India']
['Welc', 'me', 't', ' India']
What is the output of the following Python code?
s = "A:B:C"
print(s.find(':') * 2)
2
1
4
0
What is the output of the following Python code?
s = "10"
print(s.isdigit() and s.isspace())
True
False
Error
None
What is the output of the following Python code?
s = "CBSE"
new_s = s[0].lower() + s[1:]
print(new_s.title())
"Cbse"
"cbse"
"CBSE"
"C B S E"
What is the output of the following Python code?
s = "Hello-World"
print(s.replace('-', ' ').title())
"Hello world"
"Hello-World"
"Hello World"
"Hello-world"
What is the output of the following Python code?
s = "Data Science"
print(s.lower().startswith('d') and s.upper().endswith('E'))
True
False
What is the output of the following Python code?
s = "Python"
print(s.lstrip('P').rstrip('n'))
"ython"
"Pytho"
"ytho"
"yth"
What is the output of the following Python code?
s = "Exam@2024"
print(s.isalpha() or s.isdigit() or s.isalnum())
True
False
Error
None
What is the output of the following Python code?
s = "school"
print(s.count('o', 1, 5))
0
1
2
3
What is the output of the following Python code?
s = "A-B-C-D"
print(len(s.split('-', 2)))
2
3
4
5
What is the output of the following Python code?
s = "a,b,c"
print(s.replace(',', ' ', 1))
"a b,c"
"a,b c"
"a b c"
"a, b, c"
What is the output of the following Python code?
s = "12th_Class"
print(s.islower() and s.isupper())
True
False
Error
None
What is the output of the following Python code?
s = "Python"
print(s * (2 if len(s) > 5 else 1))
"Python"
"PythonPython"
"PythonPythonPython"
Error
What is the output of the following Python code?
s = "a,b,c"
print(s.split(','))
['a', 'b', 'c']
('a', 'b', 'c')
"a b c"
['a b c']
What is the output of the following Python code?
s = "hello"
print(s.index('l', 3))
2
3
4
-1
What is the output of the following Python code?
s = "My Data"
print(s.title().replace(' ', '#'))
"My#Data"
"my#data"
"My Data"
"My#data"
What is the output of the following Python code?
s = "a b c"
print(len(s.split()))
1
3
5
0
What is the output of the following Python code?
s = "PyTHoN"
print(s.swapcase().index('h'))
1
2
3
Error
What is the output of the following Python code?
s = "mississippi"
print(s.find('z'))
0
10
-1
Error
What is the output of the following Python code?
s = "hello"
print(s.endswith('lo'))
True
False
Error
None
What is the output of the following Python code?
s = "Class 12"
print(s.isalnum() or s.isspace())
True
False
Error
None
What is the output of the following Python code?
s = "abc_def"
print(s.split('_')[1].upper())
def
ABC
DEF
ABC-DEF
What is the output of the following Python code?
print(" ".join("ABC".lower()))
"a b c"
"ABC"
['a', 'b', 'c']
"abc"
What is the output of the following Python code?
s = " Python "
print(s.lstrip().rstrip().upper())
"PYTHON"
" PYTHON "
" PYTHON"
" PYTHON"
What is the output of the following Python code?
s = "DATA"
print(s.lower() == 'data')
True
False
Error
None
What is the output of the following Python code?
s = "abc"
s = s.replace('a', 'x')
print(s)
"abc"
"xbc"
"xxc"
"abc"
What is the output of the following Python code?
s = "1,2,3"
l = s.split(',')
print(len(l) + l[0].isdigit())
3
4
5
Error
What is the output of the following Python code?
s = "PyThOn"
print(s[::-1][-1].islower())
True
False
Error
None
What is the output of the following Python code?
s = "Hello"
print('l' in s and 'z' not in s)
True
False
"I"
"Z"
What is the output of the following Python code?
s = "programming"
print(s.capitalize().count('P'))
0
1
2
3
What is the output of the following Python code?
s = "A:B:C"
print(s.index(':') * s.count(':'))
2
1
4
0
What will be the output of the following Python code?
s = "Programming"
print(s[3:7])
gramm
gram
grammi
rammi
Given the string text = "Python" , which slice will produce the output "tho"? a) b) c) text[1:4] d) text[2:4]
text[2:5]
text[3:6]
text[1:4]
text[2:4]
What does s[:5] do for the string s = "ComputerScience" ?
Returns the last 5 characters.
Returns the first 5 characters.
Returns characters from index 5 to the end.
Returns characters at indices 0 and 5.
If s = "Amazing", what will s[4:] evaluate to?
azin
ing
zing
g
What is the result of the following slice?
word = "Examination"
print(word[::2])
Exaion
Eamin
Eaiai
Eaiain
Which slice expression reverses the string s = "reverse"?
s[::1]
s[-1::-1]
s[:-1:-1]
s[::-1]
What will be the output of s[-6:-2] for s = "Education"?
cat
cation
cati
Empty String
What is the output of s[8:2:-2] for s = "Mathematics"?
eai
iae
tea
Emty String
What is the output of the code?
s = "Python"
print(s[1:-1:2])
yhn
yh
yt
yton
For s = "CBSEClassXII", what is the output of s[12:0]?
IIXCsalCSEB
CBSEClassXII
CBSEClass
Empty String
Which slice expression results in the full original string s = "Example"?
s[:]
s[::]
s[::1]
All of the Above
What happens if the start index is larger than the stop index, and the step is positive?
A ValueError is raised.
The string is sliced in reverse.
The first character is returned.
An empty string is returned.
s = "python" . What is the output of s[1:5:-1]?
ohty
ohtyp
o
Empty String
The string data = "A1B2C3D4E5" represents five 2-character data points. Which slice will extract only the data 'B' and 'D'?
data[2:8:2]
data[2:6:4]
data[2:9:4]
data[1:8:4]
Given s = "P.Y.T.H.O.N", which slice extracts all the non-dot characters: "PYTHON"?
s[:-1:2]
s[0:12:2]
s[13::2]
s[:2:]
What is the correct option is it execute following statement ?
s='programming'
print(s[4:1:-1])
gamn
ram
rgo
mar
What will be the output of the following code?
s = 'computer'
temp = s[::3]
print(temp[::-1])
moc
cpe
rpu
epc
Find correct choice ?
s = 'programming'
temp = s[::-2]
print(temp[1:4])
imr
rmi
gim
mig
What will be the output of the following code?
s = 'computer'
print(s[len(s)-3:len(s)])
ter
ret
t
te
