Problem in keeping quotation marks

0
famous_person = "Dalai Lama disse:\n"

message = "\Se quer viver uma vida feliz, amarre-se a uma meta, não a pessoas nem a coisas\"

print(famous_person + message)

I have tried everything (including putting two quotes). If I put two quotes to the left it gives error, if it is to the right I have no left quotation mark ... I would like to keep both.

    
asked by anonymous 16.12.2018 / 13:19

3 answers

2

To make your code cleaner, you can set the string with single quotation marks, so the double quotes in the message will not interfere with the syntax:

texto = '"Mensagem entre aspas"'

Remembering that Python allows you to define the string in four ways: single quotes, double quotes, single quote triples, and trio of double quotation marks (the last two for multi-line string). Regardless of which one to use, the other three will be considered as text within the string.

    
16.12.2018 / 14:06
1

For quotes in a string you should use \ :

message = "\"Se quer viver uma vida feliz, amarre-se a uma meta, não a pessoas nem a coisas\""

The backslash ( \ ) serves to undo the effect of a special language symbol inside the string, leaving it only as a symbol.

    
16.12.2018 / 13:26
1

By adding a third option, you can use the """ ... """ or ''' ... ''' sequences to delimit the strings without escaping the quotation marks and apostrophes within it:

>>> a = '''Disse ele: "era uma vez um gato xadrez" '''
>>> b = """e 'todos' ficaram "perplexos" com a afirmação."""
>>> print a + b

And the result: Disse ele: "era uma vez um gato xadrez" e 'todos' ficaram "perplexos" com a afirmação.

    
17.12.2018 / 01:14