example problem | Sololearn: Learn to code for FREE!

0

example problem

>>> 'Brian\'s mother: He\'s not the Messiah. He\'s a very naughty boy!' 'Brian's mother: He's not the Messiah. He's a very naughty boy!'. i dont understand

1/24/2020 7:31:39 PM

ifrit

3 Answers

New Answer

+5

The lesson is teaching the use of escape characters when printing strings. In the sentence example there a 5 quotation marks. The outside quotation marks are meant to encapsulate the string sentence. The inner quote marks are for the English language syntax. Python will try to encapsulate the string from the first pair of quote marks, so to prevent this, an escape character is used (\) to tell Python to bypass the following quote mark. This continues to happen until a quote mark is encountered which has not been escaped. The string is then encapsulated. Try printing 'Brian's', Then try printing 'Brian\'s' You will see the difference in the output

+3

"He's very naughty." 'He\'s very naughty.' See the difference? Another example: 'She said, "Hello!" ' "She said, \"Hello!\" "

0

it was in the strings topic, i dnt understand if i have to remove \ from first line or i have to use \ where i will use 's ?