[PriP 1.1] Revision Tour-1 – Practical 1.1

|
Table of Contents

5. What would be the output of following code?

print('doesn\'t')
print("doesn\'t")
print('"Yes,"he said.')
print("\"Yes,\"he said.")

Solution:

The provided code contains print statements with various string literals. Let’s analyze each line:

  1. print('doesn\'t'): This prints the string “doesn’t” with the escape sequence \' representing a single quote. The backslash \ before the single quote escapes it, so it’s treated as a literal single quote within the string.
  2. print("doesn\'t"): Similarly, this also prints the string “doesn’t” with the escape sequence \' representing a single quote.
  3. print('”Yes,”he said.'): This prints the string ”Yes,”he said. with the characters and (curly double quotes) surrounding “Yes,”. Note that is not a regular double quote ", which may lead to unexpected output.
  4. print("”Yes,”he said."): Similarly, this also prints the string ”Yes,”he said. with the characters and (curly double quotes) surrounding “Yes,”.

Let’s print each line and see the output:

Python
print('doesn\'t')
print("doesn\'t")
print('"Yes,"he said.')
print(""Yes,"he said.")
Output
doesn't
doesn't
"Yes,"he said.
"Yes,"he said.

In the output, the strings are printed as described above. However, note that the third and fourth lines may contain characters with curly double quotes depending on the system’s font and encoding, which may appear different from regular double quotes. But, it’s been changed here to regular double quotes only so as to avoid confusion.

"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *