Problem keeping quotation marks

Asked

Viewed 563 times

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 gives error, if it is to the right I am without quotation mark left... I’d like to keep both.

  • If one of the answers below solved your problem, you can choose the one that best solved it and accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem.

3 answers

2

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

texto = '"Mensagem entre aspas"'

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

1

Adding a third option, you can use sequences """ ... """ or ''' ... ''' to delimit the strings without the need to escape the quotes 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.

1

To quote a string you must 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.

  • It worked :) Vlw by explanation, it is always good to learn kkkk

  • hehe of nothing and a pleasure to be able to help. : D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.