ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print('a,b,,c'.split(','), 'a b c'.split(), 'a b c'.split(' '), ' x '.strip(), 'x,'.split(','))- A. ['a', 'b', 'c'] ['a', 'b', 'c'] ['a', 'b', 'c'] x
- B. ['a', 'b', '', 'c'] ['a', 'b', 'c'] ['a', 'b', '', 'c'] x ['x', '']
- C. ['a', 'b', ''] ['a', 'b', 'c'] ['a', 'b', '', 'c'] x ['x', '']
- D. ['a', 'b', '', 'c'] ['a', 'c', 'b'] ['a', 'b', '', 'c'] x ['x', '']
Correct answer: B. ['a', 'b', '', 'c'] ['a', 'b', 'c'] ['a', 'b', '', 'c'] x ['x', '']
split with an explicit separator keeps empty fields, so 'a,b,,c' gives an empty string. split() with no argument collapses runs of whitespace, but split(' ') on 'a b c' keeps the empty field between the two spaces. 'x,' splits into ['x', ''].
More Python Programming questions
- What is printed? python print(sorted(['Bb', 'aA', 'bb', 'AA'], key=str.lower))
- What is printed? python print(-7 % 3, 7 % -3, -7 // 3, 7 // -3)
- What is printed? python print(pow(-7, 3, 5), (-7) 2 % 5, -7 2 % 5)
- What is printed? python print(2100 % 7, pow(2, 100, 7), pow(2, 100, 1))
- What is printed? python x = 1020 print(x // 3, x / 3 == x // 3, x % 3)
- What is printed? python n = int('9' 30) print(len(str(n + 1)), (n + 1) - 1030)