ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
s = 'hello'
print(s.find('l'), s.index('l', 3), s.rfind('l'), s.count('ll'), s.find('z'))- A. 2 3 3 2 -1
- B. 2 3 3 1 -1
- C. 2 2 3 1 -1
- D. 2 3 2 1 -1
Correct answer: B. 2 3 3 1 -1
find returns the first index of 'l' (2), index with start 3 finds the next one at 3, and rfind returns the last at 3. find returns -1 when nothing matches, while index would raise ValueError.
More Python Programming questions
- What is printed? python print('%.2e' % 0.000123456, '%G' % 1e-5, '%g' % 1e-5, '%.3g' % 1234.5)
- What is printed? python print(max(3, 3.0), min(3.0, 3), max([2, 2.0]), type(max(2.0, 2)).name)
- What is printed? python print(sum([True, True, False]), sum([1.5, True]), True 3 + 0.5, sum([1, 2], 10))
- What is printed? python print(all([]), any([]), all([0, 1]), any([0.0, '']), all(x > 0 for x in [1, -1]))
- What is printed? python print(list(zip('ab', [1, 2, 3])), dict(zip('ab', [1, 2, 3])))
- What is printed? python print(sorted([('b', 1), ('a', 2), ('a', 1)])[1:], sorted({3: 'c', 1: 'a'}.items()))