How to write multiple lines in Python?

Asked

Viewed 25,171 times

11

How can I write a string with multiple lines in Python?

As I come from PHP, I usually do so:

$string = "
uma linha
outra linha2
"

Or else:

$string = <<<EOT
uma linha
outra linha
EOT;

When I try to do this in Python, it generates an error.

Example:

string = '
uma linha
outra linha
'

Upshot:

EOL while Scanning string literal

How can I do this in Python?

  • Preferably, in an elegant way, and not like in javascript!

3 answers

14


Triple quotes:

print("""linha1
linha2
linha3
""")

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can use single quotes as long as they are tripled.

But you can’t use indentation under penalty of her being part of string.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This can also be used as doc strings. Who are nothing more than strings loose in the code. They will not be assigned to variables or printed, they will only be part of the code and this indicates that it is a documentation comment that can then be read by a specific tool.

  • Would not be Triple quotes?

  • But then the question arises: This is not used to document functions and for comments?

  • Yes, I improved this part. It’s hard to post anything quickly. It’s dirty to let SE have first class and second class users. Triple quotes do not exist. Although some calls it this way.

5

To make a string has multiple lines you can use the line break character \n. Thus:

string = 'uma linha\noutra linha'
print string

You can still use the function os.linesep which according to the documentation it makes use of the most appropriate line break character for your operating system, in case Windows would be the \r\n.

string = 'uma linha%soutra linha' % os.linesep
print string

Result for either of the above two cases:

a line
other line

See it working on Ideone.

Reference: Python Docs - linesep.


If what you need is to use multiple lines to form a string with a line you can do with \:

string = '\
uma linha \
outra linha\
'
print string

Or with ():

string = ('uma linha '
'outra linha')
print string

Or with () and the +.

string = ('uma linha ' +
'outra linha')
print string

Result for any of the above three cases:

a line another line

See it working on Ideone.

  • Good, @Math. The one from () I didn’t know :)

  • The question is a string that includes the line break characters - the answer via is the strings with triple quotes.

  • @jsbueno gee.. you are covered with reason, only now I opened the example of bigown and understood this. I will see if I repair or delete my answer (if there is a repair, but I don’t think so). Thank you for noticing this and warning.

  • You can always fix the answer. You can add the form with triple quotes to the beginning of the answer, and write: "there are other ways to continue strings in Python, without putting the line break character" - Just note that your answer does not yet cover all forms. The most commonly used, for example, is to simply open single quotes at the bottom line, without using the "+" - this concatenates the strings in the build step.

  • @jsbueno edited my question. I put two options to form a line-breaking string inside it and added your suggestion not to use +, which is actually what makes the most sense.

1

And if you want to print the values of variables on these lines, you can do so:

Remembering that I am using Python 3.5

nome = 'rafael'
idade = '21'
peso = '68'
altura = '1.70'

print (
'\nNome: ' + nome +
'\nIdade: ' + idade +
'\nPeso: ' + peso+
'\nAltura: ' + altura +
'\n'
)

Browser other questions tagged

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