ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
x = 'a,b;c'
print(x.split(',', 1), x.split(';')[0].split(','), [t for t in x.replace(';', ',').split(',') if t], x.count(',') + x.count(';'))- A. ['a', 'b', 'c'] ['a'] ['a', 'b;c'] 1
- B. ['a', 'b;c'] ['a'] ['a', 'b', 'c'] 2
- C. ['a', 'b;c'] ['a', 'b'] ['a', 'b', 'c'] 1
- D. ['a', 'b;c'] ['a', 'b'] ['a', 'b', 'c'] 2
Correct answer: D. ['a', 'b;c'] ['a', 'b'] ['a', 'b', 'c'] 2
split with maxsplit 1 stops after the first comma, so the rest stays together, including the semicolon. Splitting on the semicolon first leaves a,b, and replacing ; with , before splitting gives three items. The two counts add up to 2.
More Python Programming questions
- What is printed? python print(sorted(['10', '9', '100', '8'], key=len), sorted(['10', '9', '100', '8'], key=int))
- What is printed? python from math import isqrt print(isqrt(1030), isqrt(99), isqrt(1030 - 1))
- What is printed? python print(sum(1 for in range(105) if '7' in str()), len(str(sum(1 for in range(105) if '7' in str()))))
- What is printed? python print('%s' % (1,), '%s' % ((1,),), '%r' % ((1, 2),))
- What is printed? python print(1 if True else 0 or 2, not 0 or 3, (not 0) or 3, 0 or '' or None)
- What is printed? python print(max(map(str, [9, 10, 2])), max([9, 10, 2]), min(map(int, ['10', '9'])))