ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
s = 'a-b-c'
print(s.split('-', 1), s.rsplit('-', 1)[-1], s.partition('-')[2])- A. ['a', 'b'] c b-c
- B. ['a', 'b-c'] c b-c
- C. ['a', 'b', 'c'] c b
- D. ['a', 'b-c'] b d
Correct answer: B. ['a', 'b-c'] c b-c
split with maxsplit=1 cuts at the first hyphen only. rsplit with maxsplit=1 cuts at the last hyphen, so the final piece is c. partition gives everything after the first hyphen.
More Python Programming questions
- What is printed? python print(divmod(-1, 10), divmod(1, -10), -1 % 10)
- What is printed? python words = ['hi', 'there', 'hey'] print(max(words), min(words, key=len), sorted(words, key=lambda w: (len(w), w)))
- What is printed? python print(pow(0, 0), 0 0 == 1, pow(2, -2, 5), pow(0, 0, 5))
- What is printed? python import math print(0.0 == -0.0, 1 / float('-inf'), 2 -1074 / 2 == 0, math.copysign(0.0, -1))
- What is printed? python try: print(int('')) except ValueError: print('empty') try: print(0.0 / 0) except ZeroDivisionError: print('zero')
- What is printed? python import math try: print(math.sqrt(-1)) except ValueError: print('domain') print(float('nan') 0, math.pow(0, 0))