Python Fundamentals — Question 13
Back to all questionsQuestion 13
What will be the output produced by following code ?
(a) >>> str(print())+"One"
Output
NoneOne
Explanation
print() function doesn't return any value so its return value is None. Hence, str(print()) becomes str(None). str(None) converts None into string 'None' and addition operator joins 'None' and 'One' to give the final output as 'NoneOne'.
(b) >>> str(print("hello"))+"One"
Output
hello
NoneOne
Explanation
First, print("hello") function is executed which prints the first line of the output as hello. The return value of print() function is None i.e. nothing. str() function converts it into string and addition operator joins 'None' and 'One' to give the second line of the output as 'NoneOne'.
(c) >>> print(print("Hola"))
Output
Hola
None
Explanation
First, print("Hola") function is executed which prints the first line of the output as Hola. The return value of print() function is None i.e. nothing. This is passed as argument to the outer print function which converts it into string and prints the second line of output as None.
(d) >>> print (print ("Hola", end = " "))
Output
Hola None
Explanation
First, print ("Hola", end = " ") function is executed which prints Hola. As end argument is specified as " " so newline is not printed after Hola. The next output starts from the same line. The return value of print() function is None i.e. nothing. This is passed as argument to the outer print function which converts it into string and prints None in the same line after Hola.