0

Print the quote

Allah said,"Don't lose hope,nor be sad."

represent the allah using a variable called glorious. Then compose your message and represent it with a new variable called message. Print your message.

glorious="Allah"

print(f'{glorious} said,"don't lose hope,nor be sad."' )

But I get a syntax error.

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29

2 Answers2

1

Single quotes must be escaped with a backslash when enclosed in single quotes:

print(f'{glorious} said,"don\'t lose hope,nor be sad."')

You can also use a docstring with triple quotes instead to decrease the likelihood that you would need to escape anything in the literal:

print(f'''{glorious} said,"don't lose hope,nor be sad."''')
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Heads up that the second is just a [triple-quoted string](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals). A docstring is specifically *any* string at the head of a module, class or function definition. – MisterMiyagi Sep 06 '21 at 18:49
0

Escape the double quotes within the string:

glorious = 'allah'

print(glorious + " said, \"don't lose hope, nor be sad.\"")

You get a syntax error because for example if you do

print(glorious + "said, " don't lose hope, nor be sad.)
  1. the string is only "said, "
  2. python doesn't know what (don) is
  3. the string ('t lose hope, nor be sad.) is incomplete
sirj
  • 145
  • 1
  • 8