1

I often have to present paragraphs of text to users. I'm ok with what I know about presenting the text to the user (usually using \n or text wrapping to make it pleasing on the user side) but is there a way to have carriage returns in my actual code without \n

a = "this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user. this is a really long line of text that i need to show to the user."

b = "this is a really long line of text that i need to show to the user.\n
this is a really long line of text that i need to show to the user.\n
this is a really long line of text that i need to show to the user.\n"

So my goal is to break 'a' into aesthetically pleasing chunks in my code but still use wordwrap for displaying the text to the user. The only way I know to do that is shown in 'b' but that does not work with word wrapping

ghonke
  • 321
  • 3
  • 15

4 Answers4

3

So you want it readable in your actual code? Python will implicitly concatenate adjacent strings in code. To get it to work across lines you can use the line continuation character \, or, preferably, wrap it in parentheses.

a = ("This is a really long\n"
     "line of text that I\n"
     "need to show to the user.")

print(a)
Roger Fan
  • 4,945
  • 31
  • 38
  • iirc you don't need to wrap it in parens or use ```\```, adjacent strings concatenate across whitespace. It's been FOREVER though... EDIT: Nope, I'm wrong. Parentheses win! – Adam Smith Aug 20 '14 at 20:20
  • 1
    @AdamSmith It's not working for me currently, but that might be an IPython thing. Though, as a style point you should probably include the parentheses regardless. – Roger Fan Aug 20 '14 at 20:22
  • @AdamSmith You can prove the need for parens in any repl by just embedding the snippet in `eval`. Without the parens, it's a syntax error. – Silas Ray Aug 20 '14 at 20:27
1

You can either use triple quotes, e.g.

b = """this is a really long line of text that i need to show to the user.
this is a really long line of text that i need to show to the user.
this is a really long line of text that i need to show to the user."""
# no \n needed, but carriage returns happen on every newline

Or you can use automagic string concatenation

# THIS DOESN'T WORK
b = "this is a really long line of text that i need to show to the user."
    "this is a really long line of text that i need to show to the user."
    "this is a really long line of text that i need to show to the user."
# also no \n needed, but this is one long line of text.

Whoops, comments on Roger Fan's excellent answer reminded me that you can't do that without wrapping it in parentheses or using the line-continuation character \

b = ("this is a really long line of text that i need to show to the user."
     "this is a really long line of text that i need to show to the user."
     "this is a really long line of text that i need to show to the user.")
# OR
b = "this is a really long line of text that i need to show to the user."\
    "this is a really long line of text that i need to show to the user."\
    "this is a really long line of text that i need to show to the user."
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
1

Place the string in parentheses and then take advantage of Python's automatic concatentation of adjacent string literals. Then you can put whatever whitespace formatting around the text in the code you want.

b = ("this is a really long line of text that i need to show to the user."
     "this is a really long line of text that i need to show to the user."
     "this is a really long line of text that i need to show to the user.")
Silas Ray
  • 25,682
  • 5
  • 48
  • 63
0

You can use a "heredoc" with triple quotes:

b = 
'''
this is a really long line of text that i need to show to the user.
this is a really long line of text that i need to show to the user.
this is a really long line of text that i need to show to the user.
'''
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268