+ 4
print('print("print")') output:print("print") but print("print("print")") output:error. Why?
6 Answers
+ 16
It if for the quotation mark ("")
look,
case 1:
you can put a single quotation under a double quotation and also a double quotation under a single quotation. Here, you put a double under a single. So, the first pair (single) is taken as the normal syntax of the print function which we use to declare strings. and the second pair (double quotation) is displayed in the output.
case 2:
you must put a \ to display a double qoute in the output if the string was declared under a double quotation.
your first print satement is the normal function to display output. second "print" and "(" goes under a string. So, the first str here is "print(" then the string was closed and was reopened for ")". At the middle "print" goes undeclared and an error occurs.
Write case 2 like this,
>>>print("print(\"print\")")
output : print("print")
A very good question thođ
+ 7
At this point...
print("print("
... Python thinks that the string is finished.
I mean, wouldn't you, as a computer? (They're rather dumb.)
'Read till you find another ", that's the end of the string.'
So Python did that, not questioning your orders. Human say, PC do.
Then, suddenly, there comes something like...
print...
... that makes no sense under the assumption that the string is over.
So you get an error.
+ 2
\" will help. because python thinks " ends at first bracket.
0
print("print("print")")
print function in python gives the output for lines under the double quotation(print("print(") ,as per above code. And the remaining(print")") )is treated as invalid,because there is print word without quotes.
0
print("print("print")") is same as print ("a" b "c") and you cant print "" if you use same symbol for printing something
0
you can write
print('print ("print")')
the output will be:
print ("print")