1

I am trying to continue a dictionary value to the next line because it's too long but appears to still not be right. I have it broken up but apparently the indentation is now wrong too. Any ideas about how to best format this? enter image description here

Heats
  • 123
  • 1
  • 3
  • 10
  • 1
    Provide code that you have tried. – Rima Mar 12 '21 at 17:40
  • 1
    What do you mean by "indentation is now wrong"? You are probably missing the space between `to` and `be`. But apart from that, that looks right. – Yevhen Kuzmovych Mar 12 '21 at 17:44
  • try replacing " with ''' and make it 1 line. – Kral Mar 12 '21 at 17:46
  • [Please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341). Instead, copy the text, [edit] it into your post, and use the formatting tools like [code formatting](/editing-help#code). As well, please provide runnable code. What you've got pictured here is just a fragment. For reference see [mre]. – wjandrea Mar 12 '21 at 17:55
  • What's the problem with the indentation? That looks valid, so you shouldn't get an `IndentationError` or anything like that, but maybe you're getting a linter error/warning? For example, pycodestyle says `E128 continuation line under-indented for visual indent [pep8]`, though it goes away if you remove the parentheses. – wjandrea Mar 12 '21 at 18:00
  • 1
    @Kral *"make it 1 line"* -- how does that help? OP wants to split a long line. – wjandrea Mar 12 '21 at 18:01
  • Does this answer your question? [How can I break up this long line in Python?](https://stackoverflow.com/questions/2058925/how-can-i-break-up-this-long-line-in-python) – Gino Mempin Mar 13 '21 at 00:34

2 Answers2

0

String literal concatenation

d = {"question": ("this is "
                  "long data")}
print(d)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Rima
  • 1,447
  • 1
  • 6
  • 12
  • 1
    You don't actually need a backslash here due to [string literal concatenation](https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation) – wjandrea Mar 12 '21 at 17:53
  • Thank you for the inforamtion. – Rima Mar 12 '21 at 17:59
0
long_dict = {
    "key": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
           "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
}

EDIT:

Why this works: https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation (thanks @wjandrea )

konserw
  • 494
  • 2
  • 10
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Mar 13 '21 at 19:10