Harsh Agarwal
Harsh Agarwal

Reputation: 53

Can somebody explain the Output?

q =["hello", "there"]
q.insert(2,"How r u")
print("Result : "),q

OUTPUT:

Result :

(None, ['hello', 'there', 'How r u'])

Why it prints None?

Upvotes: 0

Views: 46

Answers (3)

BPS
BPS

Reputation: 1231

Every list of var names in python are in reality a tuple, so:

a, b is really (a, b)

so

print("Result : "),q is a tuple of values (None, q)

Upvotes: 0

Austin
Austin

Reputation: 26039

Because print returns None.

For example:

>>> print(print(1))
1
None

First it prints value and then returns None.

Explaining your code:

q =["hello", "there"]
q.insert(2,"How r u")
print(print("Result : "),q)

Steps:

Start from inner print:

print("Result : ") # -> prints Result :  and returns None

Now it becomes:

print(None, q) # -> prints None ['hello', 'there', 'How r u']   

Upvotes: 1

Rakibul Haq
Rakibul Haq

Reputation: 1398

print("Result : "),q

should be

print("Result : ", q)

Upvotes: 1

Related Questions