String
Type A
What is the result of the following expression?
print("""
""")
Answer
Question 1b
What is the result of the following expression?
text = "Test.\nNext line."
print (text)
Answer
Test.
Next line.
Question 1c
What is the result of the following expression?
print ('One', ' Two ' * 2)
print ('One ' + 'Two' * 2)
print (len('10123456789'))
Answer
One Two Two
One TwoTwo
11
Question 1d
What is the result of the following expression?
s = '0123456789'
print(s[3], ", ", s[0 : 3], " - ", s[2 : 5])
print(s[:3], " - ", s[3:], ", ", s[3:100])
print(s[20:], s[2:1], s[1:1])
Answer
3 , 012 - 234
012 - 3456789 , 3456789
Question 1e
What is the result of the following expression?
s ='987654321'
print (s[-1], s[-3])
print (s[-3:], s[:-3])
print (s[-100:-3], s[-100:3])
Answer
13
321 987654
987654 987
Question 2a
What will be the output produced by following code fragments?
y = str(123)
x = "hello" * 3
print (x, y)
x = "hello" + "world"
y = len(x)
print (y, x)
Answer
Output
hellohellohello 123
10 helloworld
Explanation
str(123) converts the number 123 to string and stores in y so y becomes "123". "hello" * 3
repeats "hello" 3 times and stores it in x so x becomes "hellohellohello".
"hello" + "world" concatenates both the strings so x becomes "helloworld". As "helloworld"
contains 10 characters so len(x) returns 10.
Question 2b
What will be the output produced by following code fragments?
x = "hello" + \
"to Python" + \
"world"
for char in x :
y = char
print (y, ' : ', end = ' ')
Answer
Output
h : e : l : l : o : t : o : : P : y : t : h : o : n : w : o : r : l : d :
Explanation
Inside the for loop, we are traversing the string "helloto Pythonworld" character by character
and printing each character followed by a colon (:).
Question 2c
What will be the output produced by following code fragments?
x = "hello world"
print (x[:2], x[:-2], x[-2:])
print (x[6], x[2:4])
print (x[2:-3], x[-4:-2])
Answer
Output
he hello wor ld
w ll
llo wo or
Explanation
x[:2] ⇒ he
x[:-2] ⇒ hello wor
x[-2:] ⇒ ld
x[6] ⇒ w
x[2:4] ⇒ ll
x[2:-3] ⇒ llo wo
x[-4:-2] ⇒ or
Question 3
Carefully go through the code given below and answer the questions based on it :
theStr = " This is a test "
inputStr = input(" Enter integer: ")
inputlnt = int(inputStr)
testStr = theStr
while inputlnt >= 0 :
testStr = testStr[1:-1]
inputlnt = inputlnt - 1
testBool = 't' in testStr
print (theStr) # Line 1
print (testStr) # Line 2
print (inputlnt) # Line 3
print (testBool) # Line 4
(i) Given the input integer 3, what output is produced by Line 1?
1. This is a test
2. This is a
3. is a test
4. is a
5. None of these
Answer
Option 1 — This is a test
(ii) Given the input integer 3, what output is produced by Line 2?
1. This is a test
2. s is a t
3. is a test
4. is a
5. None of these
Answer
Option 2 — s is a t
Explanation
As input is 3 and inside the while loop, inputlnt decreases by 1 in each iteration so the while
loop executes 4 times for inputlnt values 3, 2, 1, 0.
1st Iteration
testStr = "This is a test"
2nd Iteration
testStr = "his is a tes"
3rd Iteration
testStr = "is is a te"
4th Iteration
testStr = "s is a t"
(iii) Given the input integer 2, what output is produced by Line 3?
1. 0
2. 1
3. 2
4. 3
5. None of these
Answer
Option 5 — None of these
Explanation
Value of inputlnt will be -1 as till inputlnt >= 0 the while loop will continue executing.
(iv) Given the input integer 2, what output is produced by Line 4?
1. False
2. True
3. 0
4. 1
5. None of these
Answer
Option 2 — True
Explanation
As input is 2 and inside the while loop, inputlnt decreases by 1 in each iteration so the while
loop executes 3 times for inputlnt values 2, 1, 0.
1st Iteration
testStr = "This is a test"
2nd Iteration
testStr = "his is a tes"
3rd Iteration
testStr = "is is a te"
After the while loop finishes executing, value of testStr is "is is a te". 't' in testStr returns True as
letter t is present in testStr.
Question 4
Carefully go through the code given below and answer the questions based on it :
testStr = "abcdefghi"
inputStr = input ("Enter integer:")
inputlnt = int(inputStr)
count = 2
newStr = ''
while count <= inputlnt :
newStr = newStr + testStr[0 : count]
testStr = testStr[2:] #Line 1
count = count + 1
print (newStr) # Line 2
print (testStr) # Line 3
print (count) # Line 4
print (inputlnt) # Line 5
(i) Given the input integer 4, what output is produced by Line 2?
1. abcdefg
2. aabbccddeeffgg
3. abcdeefgh
4. ghi
5. None of these
Answer
Option 3 — abcdeefgh
Explanation
Input integer is 4 so while loop will execute 3 times for values of count as 2, 3, 4.
1st Iteration
⇒ newStr = '' + ab
newStr = newStr + testStr[0:2]
⇒ newStr = ab
⇒ testStr = cdefghi
testStr = testStr[2:]
2nd Iteration
⇒ newStr = ab + cde
newStr = newStr + testStr[0:3]
⇒ newStr = abcde
⇒ testStr = efghi
testStr = testStr[2:]
3rd Iteration
⇒ newStr = abcde + efgh
newStr = newStr + testStr[0:4]
⇒ newStr = abcdeefgh
⇒ testStr = ghi
testStr = testStr[2:]
(ii) Given the input integer 4, what output is produced by Line 3?
1. abcdefg
2. aabbccddeeffgg
3. abcdeefgh
4. ghi
5. None of these
Answer
Option 4 — ghi
Explanation
Input integer is 4 so while loop will execute 3 times for values of count as 2, 3, 4.
1st Iteration
⇒ testStr = cdefghi
testStr = testStr[2:]
2nd Iteration
⇒ testStr = efghi
testStr = testStr[2:]
3rd Iteration
⇒ testStr = ghi
testStr = testStr[2:]
(iii) Given the input integer 3, what output is produced by Line 4?
1. 0
2. 1
3. 2
4. 3
5. None of these
Answer
Option 5 — None of these
Explanation
Looking at the condition of while loop — while count <= inputlnt, the while loop will stop
executing when count becomes greater than inputlnt. Value of inputlnt is 3 so when loop stops
executing count will be 4.
(iv) Given the input integer 3, what output is produced by Line 5?
1. 0
2. 1
3. 2
4. 3
5. None of these
Answer
Option 4 — 3
Explanation
The input is converted from string to integer and after that its value is unchanged in the code so
line 5 prints the input integer 3.
(v) Which statement is equivalent to the statement found in Line 1?
1. testStr = testStr[2:0]
2. testStr = testStr[2:-1]
3. testStr = testStr[2:-2]
4. testStr = testStr - 2
5. None of these
Answer
Option 5 — None of these
Question 5
Carefully go through the code given below and answer the questions based on it :
inputStr = input(" Give me a string:")
biglnt = 0
littlelnt = 0
otherlnt = 0
for ele in inputStr:
if ele >= 'a' and ele <= 'm': # Line 1
littlelnt = littlelnt + 1
elif ele > 'm' and ele <= 'z':
biglnt = biglnt + 1
else:
otherlnt = otherlnt + 1
print (biglnt) # Line 2
print (littlelnt) # Line 3
print (otherlnt) # Line 4
print ([Link]()) # Line 5
(i) Given the input abcd what output is produced by Line 2?
1. 0
2. 1
3. 2
4. 3
5. 4
Answer
Option 1 — 0
Explanation
In the input abcd, all the letters are between a and m so the condition — if ele >= 'a' and ele <=
'm' is always true. Hence, biglnt is 0.
(ii) Given the input Hi Mom what output is produced by Line 3?
1. 0
2. 1
3. 2
4. 3
5. None of these
Answer
Option 3 — 2
Explanation
In the input Hi Mom, only two letters i and m satisfy the condition — if ele >= 'a' and ele <= 'm'.
Hence, value of littlelnt is 2.
(iii) Given the input Hi Mom what output is produced by Line 4?
1. 0
2. 1
3. 2
4. 3
5. None of these
Answer
Option 4 — 3
Explanation
In the input Hi Mom, 3 characters H, M and space are not between a and z. So for these 3
characters the statement in else part — otherlnt = otherlnt + 1 is executed. Hence, value of
otherlnt is 3.
(iv) Given the input 1+2 =3 what output is produced by Line 5?
1. 0
2. 1
3. True
4. False
5. None of these
Answer
Option 4 — False
Explanation
As all characters in the input string 1+2 =3 are not digits hence isdigit() returns False.
(v) Give the input Hi Mom, what changes result from modifying Line 1 from
if ele >= 'a' and ele <='m' to the expression
if ele >= 'a' and ele < 'm'?
1. No change
2. otherlnt would be larger
3. littlelnt would be larger
4. biglnt would be larger
5. None of these
Answer
Option 2 — otherlnt would be larger
Explanation
For letter m, now else case will be executed increasing the value of otherlnt.
Question 6
Carefully go through the code given below and answer the questions based on it :
in1Str = input(" Enter string of digits: ")
in2Str = input(" Enter string of digits: ")
if len(in1Str)>len(in2Str):
small = in2Str
large = in1Str
else:
small = in1Str
large = in2Str
newStr = ''
for element in small:
result = int(element) + int(large[0])
newStr = newStr + str(result)
large = large[1:]
print (len(newStr)) # Line 1
print (newStr) # Line 2
print (large) # Line 3
print (small) # Line 4
(i) Given a first input of 12345 and a second input of 246, what result is produced by Line 1?
1. 1
2. 3
3. 5
4. 0
5. None of these
Answer
Option 2 — 3
Explanation
As length of smaller input is 3, for loop executes 3 times so 3 characters are added to newStr.
Hence, length of newStr is 3.
(ii) Given a first input of 12345 and a second input of 246, what result is produced by Line 2?
1. 369
2. 246
3. 234
4. 345
5. None of these
Answer
Option 1 — 369
Explanation
For loop executes 3 times as length of smaller input is 3.
1st Iteration
⇒ result = 3
result = 2 + 1
⇒ newStr = '3'
newStr = '' + '3'
large = 2345
2nd Iteration
⇒ result = 6
result = 4 + 2
⇒ newStr = '36'
newStr = '3' + '6'
large = 345
3rd Iteration
⇒ result = 9
result = 6 + 3
⇒ newStr = '369'
newStr = '36' + '9'
large = 45
Final value of newStr is '369'.
(iii) Given a first input of 123 and a second input of 4567, what result is produced by Line 3?
1. 3
2. 7
3. 12
4. 45
5. None of these
Answer
Option 2 — 7
Explanation
For loop executes 3 times as length of smaller input is 3. Initial value of large is 4567.
1st Iteration
⇒ large = 567
large = large[1:]
⇒ large = 67
2nd Iteration large = large[1:]
⇒ large = 7
3rd Iteration large = large[1:]
(iv) Given a first input of 123 and a second input of 4567, what result is produced by Line 4?
1. 123
2. 4567
3. 7
4. 3
5. None of these
Answer
Option 1 — 123
Question 7a
Find the output if the input string is 'Test'.
S = input("Enter String :")
RS = " "
for ch in S :
RS = ch + RS
print(S + RS)
Answer
Output
TesttseT
Explanation
The for loop reverses the input string and stores the reversed string in variable RS. After that
original string and reversed string are concatenated and printed.
Question 7b
Find the output if the input string is 'Test'.
S = input("Enter String :")
RS = " "
for ch in S :
RS = ch + 2 + RS
print(S + RS)
Answer
The program gives an error at line RS = ch + 2 + RS. The operands to + are a mix of string and
integer which is not allowed in Python.
Question 8a
Find the errors. Find the line numbers causing errors.
1. S = "PURA VIDA"
2. print(S[9] + S[9 : 15])
Answer
The error is in line 2. Length of string S is 9 so its indexes range for 0 to 8. S[9] is causing error as
we are trying to access out of bound index.
Question 8b
Find the errors. Find the line numbers causing errors.
1. S = "PURA VIDA"
2. S1 = S[: 10] +S[10 :]
3. S2 = S[10] + S[-10]
Answer
The error is in line 3. Length of string S is 9 so its forward indexes range for 0 to 8 and backwards
indexes range from -1 to -9. S[10] and S[-10] are trying to access out of bound indexes.
Question 8c
Find the errors. Find the line numbers causing errors.
1. S = "PURA VIDA"
2. S1 = S * 2
3. S2 = S1[-19] + S1[-20]
4. S3 = S1[-19 :]
Answer
The error is in line 3. S1[-19] and S1[-20] are trying to access out of bound indexes.
Question 8d
Find the errors. Find the line numbers causing errors.
1. S = "PURA VIDA"
2. S1 = S[: 5]
3. S2 = S[5 :]
4. S3 = S1 * S2
5. S4 = S2 + '3'
6. S5 = S1 + 3
Answer
The errors are in line 4 and line 6. Two strings cannot be multiplied. A string and an integer
cannot be added.
Question 9
What is the output produced?
(i) >>> "whenever" .find("never")
(ii) >>> "whenever" .find("what")
Answer
(i) 3
The starting index of substring "never" in "whenever" is 3.
(ii) -1
Substring "what" is not present in "whenever".
Question 10
What is the output produced?
(i) >>> "-".join(['123','365','1319'])
(ii) >>> " ".join(['Python', 'is', 'fun'])
Answer
(i) '123-365-1319'
(ii) 'Python is fun'
Question 11
Given a string S, write expressions to print
1. first five characters of S
2. Ninth character of S
3. reversed S
4. alternate characters from reversed S
Answer
1. print(S[:5])
2. print(S[8])
3. for a in range(-1, (-len(S) - 1), -1) :
print(S[a], end = '')
4. for a in range(-1, (-len(S) - 1), -2) :
print(S[a], end = '')