Computer Science

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 

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

Python String Manipulation

6 Likes

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"

Answered By

2 Likes


Related Questions