Computer Science
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])
Python
Python String Manipulation
31 Likes
Answer
he hello wor ld
w ll
llo wo or
Working
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
Answered By
19 Likes
Related Questions
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)
What will be the output produced by following code fragments?
x = "hello" + \ "to Python" + \ "world" for char in x : y = char print (y, ' : ', end = ' ')
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 1?
- This is a test
- This is a
- is a test
- is a
- None of these
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?
- This is a test
- s is a t
- is a test
- is a
- None of these